Pandas 数据结构之 Series使用教程

377 阅读4分钟

一. Series 简介

Series是一种类似于一维数组的对象,是由一组数据(各种NumPy数据类型)以及一组与之相关的数据标签(即索引)组成。仅由一组数据也可产生简单的Series对象 Series 总的来说就是带标签的一维数组,可存储整数、浮点数、字符串、Python对象等类型的数据。标签轴通常叫做索引。

二. 实例化 Series

2.1 使用一维数组实例化

用一维数组实例化Series时,索引长度必须与数组长度一致。没有指定索引时,Pandas会帮我们创建默认的数值型索引。

In [1]: s1 = pd.Series([1, 2, 3, 4])
Out[1]:
0	1
1	2
2	3
3	4
dtype: int64

In [2]: s2 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
Out[2]:
a	1
b	2
c	3
d	4
dtype: int64

注意: Pandas 是支持重复索引的。但我们也可以重置索引,具体操作方法在后续章节中会给出。

2.2 使用字典实例化

使用字典实例化Series时, 如果未传入索引,则索引的值为字典的key:

In [1]: pd.Series({'i': 0, 'j': 1, 'k': 2})
Out[1]: 
i    0
j    1
k    2
dtype: int64

2.3 使用标量例化

使用标量值实例化时,必须提供索引。Series 按索引长度重复该标量值。

In [1]: pd.Series(6, index=[0, 1, 2])
Out[1]: 
0    6
1    6
2    6
dtype: int64

三.Series 简单使用

3.1 为Series添加Name属性

在实例化Series时,可以传入name参数为Series添加name属性。同时,Seires也支持重命名:

In [1]: s = pd.Series(6, index=[0, 1, 2], name='six')
Out[1]: 
0    6
1    6
2    6
Name: six, dtype: int64

In [2]: s.name
Out[2]: 'six'

In [3]: s = s.rename('sixsixsix')
In [4]: s.name
Out[4]: 'sixsixsix'

3.2 基于位置的切片

Series提供了类似于Python列表的切片方式:

In[0]: s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In[1]: s[0:2] 	#取下标为0和1的两个数据(不包括2,也就是从第一个开始取,取两个数据)
Out[1]:
a    1
b    2
dtype: int64

In[2]: s[:3] 	#取前三个数据
Out[2]:
a    1
b    2
c    3
dtype: int64

In[3]: s[-2:] 	#取最后两个数据(也可以理解为从倒查第二个数据一直取到末尾)
Out[3]:
c    3
d    4
dtype: int64

In[4]: s[[0,2,3]] 	#取第1、3、4这个三个数据(注意下标是从0开始的,转换为位置时需+1)
Out[4]:
a    1
c    3
d    4
dtype: int64		#注意:如果输入的位置大于列表的长度则会报出“indexers are out-of-bounds”异常

3.3 基于索引的切片

Series可使用索引标签的值来提取值:

In [0]s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In [1]: s['a'] 	#提取s中,标签为a的值
Out[1]a    1
dtype: int64

In [1]: s[['a', 'b', 'c']] 	#提取s中,标签为a, b, c的值
Out[1]a    1
b    2
c    3
dtype: int64

如果传入的索引标签的值不在Seires的轴索引中,那将会报 KeyError 异常,这里建议大家使用Series的 get 方法获取值,如果不存在,则会返回None,同时也可设置default参数,用于不存在时的默认返回值。

In [0]: s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In [1]: s['f'] 	#提取s中,标签为f的值, f不存在,将会报出异常
Out[1]:KeyError

In [2]:s.get('f') #提取s中,标签为f的值, 若f不存在,默认返回None
Out[2]:None

In [3]:s.get('f'. default=-1) #提取s中,标签为f的值, 若f不存在,返回-1
Out[3]:-1

3.4 基于条件的切片

In[0]: s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In[1]: s[s < 2] 	#提取s中,小于2的值
Out[1]:
a    1
b    2
dtype: int64

In[1]: s[s> s.mean()] 	#提取s中,大于平均数的值
Out[1]:
c    3
d    4
dtype: int64

In[1]: s[s.between(1, 3, inclusive=False)] 	#提取s中,值介于1,3之间的数据(不包含1,3)
Out[1]:
b    2
dtype: int64

在提取区间数据时,如果想让两端的值包含其中(满足两端的值也被提取出来),只需要把 inclusive 参数的值赋为True

3.5 其他操作

Series 不用循环也可以像操作单个数值一样快速进行数学运算:

In[0]: s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In[1]: s + s
Out[1]a    2
b    4
c    6
d    8
dtype: int64

In[2]: s - 1
Out[2]a    0
b    1
c    2
d    3
dtype: int64

Series 之间的操作会自动 基于标签 对齐数据. 如果一个Series中的标签在另一个Series中不存在,那么计算得到的结果将是NaN,即缺失值,有缺失值NaN的处理在后续章节也会讲到。因此,我们不用顾及执行操作的Series是否有相同的标签。 Pandas数据结构集成的数据对齐的功能,是Pandas区别于大多数标签型数据处理工具的重要特性。

In[0]: s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In[0]: s2 = pd.Series([3, 6, 11], index=['a', 'b', 'f'])
In[1]: s1 + s2
Out[1]:
a   4.0
b   8.0
c   NaN
d   NaN
f   NaN
dtype: float64