如何创建Series对象
常见的创建Pandas对象的方式,都像这样的形式:
pd.Series(data, index=index)
- 1
其中,index是一个可选参数,data参数支持多种数据类型
例如,data可以时列表或Numpy数组,这是index默认值为整数序列:
pd.Series([2,4,6])
Out[58]:
0 2
1 4
2 6
dtype: int64
- 1
- 2
- 3
- 4
- 5
- 6
- 7
data也可以是一个标量,创建Series对象时会重复填充到每一个索引上:
pd.Series(5, index=[100, 200, 300])
Out[59]:
100 5
200 5
300 5
dtype: int64
- 1
- 2
- 3
- 4
- 5
- 6
- 7
data开可以是一个字典,index默认是字典键:
pd.Series({2:'a', 1:'b', 3:'c'})
Out[60]:
2 a
1 b
3 c
dtype: object
- 1
- 2
- 3
- 4
- 5
- 6
- 7
每一种形式都可以通过显示指定索引筛选需要的结果:
pd.Series({2:'a', 1:'b', 3:'c'}, index=[3,2])
Out[61]:
3 c
2 a
dtype: object
- 1
- 2
- 3
- 4
- 5
- 6