如何创建Series对象

如何创建Series对象

常见的创建Pandas对象的方式,都像这样的形式:

pd.Series(data, index=index)

其中,index是一个可选参数,data参数支持多种数据类型

例如,data可以时列表或Numpy数组,这是index默认值为整数序列:

pd.Series([2,4,6])

Out[58]: 
0    2
1    4
2    6
dtype: int64

data也可以是一个标量,创建Series对象时会重复填充到每一个索引上:

pd.Series(5, index=[100, 200, 300])

Out[59]: 
100    5
200    5
300    5
dtype: int64

data开可以是一个字典,index默认是字典键:

pd.Series({2:'a', 1:'b', 3:'c'})

Out[60]: 
2    a
1    b
3    c
dtype: object

每一种形式都可以通过显示指定索引筛选需要的结果:

pd.Series({2:'a', 1:'b', 3:'c'}, index=[3,2])

Out[61]: 
3    c
2    a
dtype: object

猜你喜欢

转载自blog.csdn.net/jasonzhoujx/article/details/81612438