首先介绍第一类:Series (注意大小写)
defination:Series is a one-dimensional labeled array capable of holding any data type(integers,strings,floating point numbers,python objects,etc.)
定义:Series是一维带标签的数组,其可包含任意类型的元素(整数,字符串,浮点数,Python对象等)
<1>创建Series对象的典型方法是:
import pandas as pd
import numpy as np
dic={'a':1,'b':2,'c':3}
p1=pd.Series(dic,index=['b','a','d','c','f'])
print(p1)
output is:
b 2
a 1
d NaN
c 3
f NaN
dtype: float64
p2=pd.Series([1,2],index=['apple','orange'])
print(p2)
>>>
apple 1
orange 2
dtype: int64
p3=pd.Series(6,index=range(3))
print(p3)
>>>
0 6
1 6
2 6
dtype: int64
- 对Series的操作非常类似于对ndarray的操作
import numpy as np
dic={'a':1,'b':2,'c':3}
p1=pd.Series(dic,index=['b','a','d','c','f'])
print(p1)
output is:
b 2
a 1
d NaN
c 3
f NaN
dtype: float64
b 0.676150
c -0.966844
d 1.123409
e 12.000000
dtype: float64
b 1.236786
c -1.338008
d -2.445264
e 24.000000
dtype: float64
b 1.056986
c -2.799877
d -3.421514
e 24.000000
dtype: float64
b -1.721291
d 0.410060
e NaN
dtype: float64
c 0.802023
d 1.354568
dtype: float64
1 -0.736353
2 -1.202390
3 -0.101308
4 0.017583
Name: something, dtype: float64
print(s.name)
>>>
'something'
对序列Series重命名可采用pandas.Series.rename()
s2=s.rename('difference')
print(s2.name)
>>>
'difference'
注意:s和s2是不同的对象。