使用来自另一个数据框的数据更新 pandas 数据框 [英] Update a pandas dataframe with data from another dataframe
问题描述
我有两个类似的DataFrame.
I've got two similar DataFrames.
df1.head()
1 2 3 4
3234 Lorum Ipsum Foo Bar
8839 NaN NaN NaN NaN
9911 Lorum Ipsum Bar Foo
2256 NaN NaN NaN NaN
df2.head()
1 3 4
8839 Lorum Ipsum Foo
2256 Lorum Ipsum Bar
我想基于相同的索引和列合并/更新两者以更新NaN值.
I'd like to merge/update the two based on same index and column to update the NaN values.
理想的结果:
df3.head()
1 2 3 4
3234 Lorum Ipsum Foo Bar
8839 Lorum NaN Ipsum Foo
9911 Lorum Ipsum Bar Foo
2256 Lorum NaN Ipsum Bar
df2
并不像df1
那样包含所有列,但是它确实包含匹配的列并且具有匹配的索引.
df2
doesn't contain all of the columns as df1
but the columns it does contain match and it has matching indexes.
我已经尝试过了:
df3 = df1.update(df2)
但是还没有成功.我一直在查看文档,认为pd.merge
或pd.concat
可能会有所帮助,但我有些困惑.
But haven't had any success. I've been looking at the docs and think pd.merge
or pd.concat
may help but I'm a bit confused.
谢谢
推荐答案
您可以使用 reindex
:
You can use combine_first
with reindex
:
df3 = df2.combine_first(df1).reindex(df1.index)
print (df3)
1 2 3 4
3234 Lorum Ipsum Foo Bar
8839 Lorum NaN Ipsum Foo
9911 Lorum Ipsum Bar Foo
2256 Lorum NaN Ipsum Bar
或使用您的解决方案,但 update
在原位工作,因此,如果将其分配给变量,则返回None
:
Or use your solution, but update
working inplace, so if assign to variable it return None
:
df1.update(df2)
print (df1)
1 2 3 4
3234 Lorum Ipsum Foo Bar
8839 Lorum NaN Ipsum Foo
9911 Lorum Ipsum Bar Foo
2256 Lorum NaN Ipsum Bar
print (df1.update(df2))
None
这篇关于使用来自另一个数据框的数据更新 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!