如何去除NAN(not a number):
dropna
fillna
add及fill_value
#当计算结果为NAN时,需要用特定的值来补充代替 import pandas as pd s1=pd.Series([1,2,3,4],index=['a','b','c','d']) s2=pd.Series([10,20,30,40],index=['c','d','e','f']) s3=pd.Series([1,2,3,4],index=['a','b','c','d']) s4=pd.Series([10,20,30,40],index=['c','d','e','f']) print (s1+s2) #--1--直接相加只有索引c\d对应有值 print ((s1+s2).dropna())#--2--去除值为空的 print ((s1+s2).fillna(0))#--3--把值为空的地方填充成0 print (s1.add(s2,fill_value=1)) #--4--s1和s2相加,当索引对应值不存在,用1代替s2中元素和s1相加
-----------1------------ a NaN b NaN c 13.0 d 24.0 e NaN f NaN dtype: float64
----------2-------------- c 13.0 d 24.0 dtype: float64 -----------3-------------- a 0.0 b 0.0 c 13.0 d 24.0 e 0.0 f 0.0 dtype: float64 -----------4--------------- a 2.0 b 3.0 c 13.0 d 24.0 e 31.0 f 41.0 dtype: float64