简体   繁体   English

Python Pandas DF 按索引将 NaN 替换为其他 DF 的值

[英]Python Pandas DF Replace NaN with value from other DF by Index

I have two Dataframes where the Index can be set to ['Date', 'Name'].我有两个数据框,其中索引可以设置为 ['Date', 'Name']。 I now want to replace the NaN in the first Dataframe for all common columns with the Data which can be found in the second one (which can also have NaN values in the columns).我现在想用可以在第二个中找到的 Data 替换所有常见列的第一个 Dataframe 中的 NaN(列中也可以有 NaN 值)。 They look like this:它们看起来像这样:

NaN = np.nan
df1 = pd.DataFrame([
    ['2020-01-01', 'Foo1', 8, 0.999],
    ['2020-01-01', 'Bar1', NaN, NaN],
    ['2020-01-02', 'Foo1', 1, 0.564],
    ['2020-01-03', 'Foo1', NaN, NaN]],
    columns=['Date', 'Name', 'Val1', 'Val2'])

df2 = pd.DataFrame([
    ['2020-01-01', 'Foo1', 8, 0.999],
    ['2020-01-01', 'Bar1', 5, 0.6],
    ['2020-01-02', 'Foo1', 1, 0.564],
    ['2020-01-03', 'Foo1', NaN, NaN]],
    columns=['Date', 'Name', 'Val1', 'Val2'])

I tried to do it with where and replace statement but unfortunately I can't figure it out nor find anything which helped here so far.我试图用 where 和 replace 语句来做,但不幸的是我无法弄清楚,也找不到任何对这里有帮助的东西。 Thanks a lot in advance!提前非常感谢!

Try with combine_first or fillna尝试使用combine_firstfillna

df1 = df1.set_index(['Date','Name']).combine_first(df2.set_index(['Date','Name'])).reset_index()
df1
         Date  Name  Val1   Val2
0  2020-01-01  Foo1   8.0  0.999
1  2020-01-01  Bar1   5.0  0.600
2  2020-01-02  Foo1   1.0  0.564
3  2020-01-03  Foo1   NaN    NaN

df1 = df1.set_index(['Date','Name']).fillna(df2.set_index(['Date','Name'])).reset_index()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM