一、pandas中的Series模块
Series 是一个类似数组的数据结构
- 可以把Series看成一个定长的有序字典
可以通过shape,size,index,values等得到series的属性 - 可以通过head(),tail()快速查看Series对象的样式
- 当索引没有对应的值时,可能出现缺失数据显示NaN(not a number)的情况
- 可以使用pd.isnull(),pd.notnull(),或自带isnull(),notnull()函数检测缺失数据
- Series对象本身及其索引都有一个name属性
Series的创建
由列表或numpy数组创建
默认索引为0到N-1的整数型索引
obj = Series([1,2,3,4])
print(obj)
输出:
0 1
1 2
2 3
3 4
dtype: int64
#还可以通过设置index参数指定索引
obj2 = Series([1,2,3,4],index=['a','b','c','d'])
obj2
输出:
a 1
b 2
c 3
d 4
dtype: int64
特别地,由ndarray创建的是引用,而不是副本。对Series元素的改变也会改变原来的ndarray对象中的元素。(列表没有这种情况)
a = np.array([1,2,3])
obj = Series(a)
obj
输出:
0 1
1 2
2 3
dtype: int64
obj[0]=0
print(a)
print(obj)
输出:
[0 2 3]
0 0
1 2
2 3
dtype: int64
用字典进行创建
obj = Series({'a':1,'b':2})
obj
输出:
a 1
b 2
dtype: int
Series的索引和切片
- (1) 显式索引:
- 使用index中的元素作为索引值
- 使用.loc[](推荐)
注意,此时是闭区间
- (2) 隐式索引:
- 使用整数作为索引值
- 使用.iloc[](推荐)
注意,此时是半开区间
Series的运算
- 适用于numpy的数组运算也适用于Series
- Series之间的运算 在运算中自动对齐不同索引的数据 如果索引不对应,则补NaN