Pandas —— combine

xiaoxiao2021-02-28  29

先创建两组数据a和b

In [62]: a=pd.Series([np.nan,2.5,np.nan,3.5,4.5,np.nan],index=['f','e','d','c','b','a']) In [63]: b=pd.Series([1,np.nan,3,4,5,np.nan],index=['f','e','d','c','b','a']) In [64]: a Out[64]: f NaN e 2.5 d NaN c 3.5 b 4.5 a NaN dtype: float64 In [65]: b Out[65]: f 1.0 e NaN d 3.0 c 4.0 b 5.0 a NaN dtype: float64

用a的数据填补b的缺失值

In [66]: b.combine_first(a) Out[66]: f 1.0 e 2.5 d 3.0 c 4.0 b 5.0 a NaN dtype: float64

用b的数据填补a的缺失值

In [67]: a.combine_first(b) Out[67]: f 1.0 e 2.5 d 3.0 c 3.5 b 4.5 a NaN dtype: float64
转载请注明原文地址: https://www.6miu.com/read-1700197.html

最新回复(0)