pandas数据类型之Series如何使用

发布时间:2022-08-08 10:54:59 作者:iii
来源:亿速云 阅读:124

这篇文章主要介绍“pandas数据类型之Series如何使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“pandas数据类型之Series如何使用”文章能帮助大家解决问题。

pandas中包含了DataFrame和Series数据类型,分别表示二维数据结构和一维数据结构。
简单的可以理解为Series为excel表的某一行或者列,DataFrame是多行多列的区域。

Series类型

Series的三种创建方式

通过数组创建Series

import pandas as pd
import numpy as np
s1 = pd.Series([1,2,3,'tom',True])
s2 = pd.Series(range(0, 10, 1))
print(s1)
print(s2)
print(type(s1), type(s2))

创建指定索引列的Series

索引为数组

s1 = pd.Series([1,2], index=["a", "b"])
s2 = pd.Series(range(10,15,1), index=list('ngjur'))
s3 = pd.Series(range(100,110,2), index=range(4,9,1))
print(s1)
print(s2)
print(s3)
print(s1["a"], s1[1])    #位置索引从0开始
print(s2["r"], s2[-2])   #位置索引从0开始,可以用和列表同样的索引访问方式,-1表示最后一个元素
print(s3[4])    #当定义的索引为数字时,会覆盖之前位置索引的方式,也就是说s3[0]到s3[3],s3[-1]将不能再访问。

a    1
b    2
dtype: int64
n    10
g    11
j    12
u    13
r    14
dtype: int64
4    100
5    102
6    104
7    106
8    108
dtype: int64
1 2
14 13
100

使用字典创建

key为标签索引,value为series的每个元素的值

s1 = pd.Series({'tom':'001', 'jack':'002'})
print(s1)

tom     001
jack    002
dtype: object

标量创建Series对象

如果data是标量值,则必须提供索引

s1 = pd.Series(5, [0, 1, 2, "a"])
print(s1[[1, "a"]])

1    5
a    5
dtype: int64

Series的常见操作

Series的值访问

series_name[],[]内可以为单个位置索引或者标签索引,也可以为位置切片或者标签切片,也可以为位置索引列表或者标签索引列表

s1 = pd.Series({'tom':'001', 'jack':'002', "Jim":"003"})
s2 = s1[["tom", "jack"]]    #使用标签索引列表
s3 = s1[0:3]  # 使用位置切片
s4 = s1["tom":"Jim"]    #使用标签切片
s5 = s1[[0,1]]
print("s1-----\n", s1["tom"], type(s1[1]))  
print("s2-----\n", s2, type(s2))  #使用标签索引列表
print("s3-----\n", s3, type(s3))  #使用位置切片
print("s4-----\n", s4, type(s4))  #使用标签切片
print("s5-----\n", s5, type(s5))  #使用位置索引列表

s1-----
 001 <class 'str'>
s2-----
 tom     001
jack    002
dtype: object <class 'pandas.core.series.Series'>
s3-----
 tom     001
jack    002
Jim     003
dtype: object <class 'pandas.core.series.Series'>
s4-----
 tom     001
jack    002
Jim     003
dtype: object <class 'pandas.core.series.Series'>
s5-----
 tom     001
jack    002
dtype: object <class 'pandas.core.series.Series'>

访问整个series

s1 = pd.Series({'tom':'001', 'jack':'002', "Jim":"003"})
s2 = s1.values
print("s2-----\n", s2, type(s2))  
s3 = pd.Series({'tom':90, 'jack':40, "Jim":100})

s2-----
 ['001' '002' '003'] <class 'numpy.ndarray'>
s2-----
 [ 90  40 100] <class 'numpy.ndarray'>

获取索引列

series_name.index
s1 = pd.Series(['tom', 'jack', "Jim"], [90, 100, 60])
print("s1-----\n", s1, type(s1))
s1_index = s1.index
print("s1_index-----\n", s1_index, type(s1_index))
print("s1_name:", s1.name)

s1-----
 90      tom
100    jack
60      Jim
dtype: object <class 'pandas.core.series.Series'>
s1_index-----
 Int64Index([90, 100, 60], dtype='int64') <class 'pandas.core.indexes.numeric.Int64Index'>
s1_name----- None

设置名称

如果 Series 用于生成 DataFrame,则 Series 的名称将成为其索引或列名称

s1 = pd.Series(np.arange(5), name='ABC',index=['a','b','c','d','e'])
print(s1)

a    0
b    1
c    2
d    3
e    4
Name: ABC, dtype: int32

Series数据编辑

Series数据删除

使用series_name.drop(),指明index,可以为标签索引,或者多个标签索引多个组成的列表,不能为位置索引,或者切片

Series数据删除

drop方法
s1 = pd.Series(np.arange(5), name='A',index=['a','b','c','d','e'])
print(s1)
# 单个值删除,指明标签索引
s1.drop('c',inplace=False)    #inplace为False不改变原s1的内容
print("删除单个值,不改变s1:\n",s1)
# 多个值删除,指明标签索引列表
s1.drop(['c','e'],inplace=False)

a    0
b    1
c    2
d    3
e    4
Name: A, dtype: int32
删除单个值,不改变s1:
 a    0
b    1
c    2
d    3
e    4
Name: A, dtype: int32

a    0
b    1
d    3
Name: A, dtype: int32

# multiindex值的删除
midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
                             ['speed', 'weight', 'length']],
                     codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
                            [0, 1, 2, 0, 1, 2, 0, 1, 2]])
s1 = pd.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3],
              index=midx)
print(s1)
s1.drop(labels='weight', level=1)

lama    speed      45.0
        weight    200.0
        length      1.2
cow     speed      30.0
        weight    250.0
        length      1.5
falcon  speed     320.0
        weight      1.0
        length      0.3
dtype: float64


lama    speed      45.0
        length      1.2
cow     speed      30.0
        length      1.5
falcon  speed     320.0
        length      0.3
dtype: float64

pop方法

pop(x), 指定要pop的标签索引

s1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
s1.pop("a")
print(s1)

b    2
c    3
dtype: int64

del方法
del s1[x], 指定要删除的吗标签索引
s1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
del s1["a"]
print(s1)

b    2
c    3
dtype: int64

Series数据添加

类似于字典中元素的添加方式

s1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
s1["d"] = 4
print(s1)

a    1
b    2
c    3
d    4
dtype: int64

append方法

s1 =pd.Series(["北京", "上海", "台湾", "香港"])
index_list =["a", "b", "c", "d"]
s1.index = index_list
print("s1-----------\n", s1)
s2 = pd.Series({"e": "广州", "f": "深圳"})
print("s2-----------\n", s2)
s3 = s1.append(s2)
print("s3-----------\n", s3)
print(s1)
s4 = s1.append(s2, ignore_index=True)
print("s4-----------\n", s4)

s1-----------
 a    北京
b    上海
c    台湾
d    香港
dtype: object
s2-----------
 e    广州
f    深圳
dtype: object
s3-----------
 a    北京
b    上海
c    台湾
d    香港
e    广州
f    深圳
dtype: object
a    北京
b    上海
c    台湾
d    香港
dtype: object
s4-----------
 0    北京
1    上海
2    台湾
3    香港
4    广州
5    深圳
dtype: object

关于“pandas数据类型之Series如何使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. pandas中的Series和DataFrame
  2. pandas中的series数据类型详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pandas series

上一篇:C++ OpenCV怎么实现数字识别功能

下一篇:怎么使用纯C语言实现异常捕获try-catch组件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》