pandas.Dataframe:填充另一个DF的缺失值

时间:2013-03-17 23:38:09

标签: python pandas dataframe missing-data

例如我有这个:

df_1 = pd.DataFrame({'A': [3 for _ in xrange(10)],
                     'B': [3 for _ in xrange(10)],
                     'C': [3 for _ in xrange(10)]})
df_2 = pd.DataFrame({'A': [2 for _ in xrange(5)],
                     'B': [2 for _ in xrange(5)],
                     'C': [2 for _ in xrange(5)]})

print(df_1 - df_2)

并且输出将按预期进行:

    A   B   C
0   1   1   1
1   1   1   1
2   1   1   1
3   1   1   1
4   1   1   1
5 NaN NaN NaN
6 NaN NaN NaN
7 NaN NaN NaN
8 NaN NaN NaN
9 NaN NaN NaN

如何使用df_1

中的值填充新创建的DF中的缺失值

1 个答案:

答案 0 :(得分:4)

您可以使用combine_first填写缺失值:

In [2]: (df_1 - df_2).combine_first(df_1)
Out[2]:
   A  B  C
0  1  1  1
1  1  1  1
2  1  1  1
3  1  1  1
4  1  1  1
5  3  3  3
6  3  3  3
7  3  3  3
8  3  3  3
9  3  3  3