Pandas Series的向量计算

#pandas做计算都是依据索引,和位置没有关系
import pandas as pd
s1=pd.Series([1,2,3,4],index=['a','b','c','d'])
s2=pd.Series([10,20,30,40],index=['a','b','c','d'])
s3=pd.Series([10,20,30,40],index=['d','b','a','c'])#索引相同的地方进行相加
s4=pd.Series([10,20,30,40],index=['c','d','e','f'])#没有同时存在的索引,得到NaN
s5=pd.Series([10,20,30,40],index=['e','f','g','h'])#索引都不同时存在,所有都是NaN
print (s1+s2)
print (s1+s3)
print (s1+s4)
print (s1+s5)
a    11
b    22
c    33
d    44
dtype: int64
a    31
b    22
c    43
d    14
dtype: int64
a     NaN
b     NaN
c    13.0
d    24.0
e     NaN
f     NaN
dtype: float64
a   NaN
b   NaN
c   NaN
d   NaN
e   NaN
f   NaN
g   NaN
h   NaN
dtype: float64
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页