python pandas 序列的生成

原文:

http://pandas.pydata.org/pandas-docs/stable/dsintro.html


pandas序列是一维有序数组,元素的类型可为整数、字符串、浮点数、python中的对象等等。例子运行环境:python3,pycharm

生成序列的方法:

s = pd.Series(data, index=index)

data:可以是很多种类型数据,比如字典、数组、标量值等等;

index:是data的索引标签,序列的索引不唯一,即可以有重复的索引。若从数组生成序列,则index值若无则默认的0,1等,也可以自定义,若为字典,则为字典的key。

若data为标量值,则所以必须要有;

从nparray生成序列的例子:

import numpy as np
import pandas as pd

s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
print(s)
a   -1.601721
b   -1.477830
c    0.483496
d    0.188437
e   -0.464686
dtype: float64

字典数据结构生成序列的例子:

此时序列的索引为字典的索引,若无此索引,则生成空值;

import pandas as pd
d = {'a' : 0., 'b' : 1., 'c' : 2.}
print(pd.Series(d))
print(pd.Series(d,index = ['a','c','d','b']))
 
 
a    0.0
b    1.0
c    2.0
dtype: float64
a    0.0
c    2.0
d    NaN
b    1.0
dtype: float64


标量生成序列:

import pandas as pd
print(pd.Series(5., index=['a', 'b', 'c', 'd', 'e']))
a    5.0
b    5.0
c    5.0
d    5.0
e    5.0
dtype: float64






















猜你喜欢

转载自blog.csdn.net/mengdiefeixumyname/article/details/78733462
今日推荐