pandas - 合并两个具有相同列名称的DataFrames [英] Pandas - merge two DataFrames with Identical Column Names

查看:523
本文介绍了 pandas - 合并两个具有相同列名称的DataFrames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据帧在第一列中具有相同的列名称和相同的ID。除了ID列之外,在一个DataFrame中包含值的每个单元格都包含另一个数据库中的NaN。
这是一个他们看起来像的例子:

  ID Cat1 Cat2 Cat3 
1 NaN 75 NaN
2 61 NaN 84
3 NaN NaN NaN


ID Cat1 Cat2 Cat3
1 54 NaN 44
2 NaN 38 NaN
3 49 50 53

我想将它们合并到一个DataFrame中,同时保留相同的列名。所以结果将如下所示:

  ID Cat1 Cat2 Cat3 
1 54 75 44
2 61 38 84
3 49 50 53

我试过:

  df3 = pd.merge(df1,df2,on ='ID',how ='outer')
/ pre>

其中给了我一个包含两倍的列的DataFrame。如何将每个DataFrame的值合并为一个?

解决方案

您可能希望 df.update 。请参阅文档

  df1.update(df2,raise_conflict = True)


I have two Data Frames with identical column names and identical IDs in the first column. With the exception of the ID column, every cell that contains a value in one DataFrame contains NaN in the other. Here's an example of what they look like:

ID    Cat1    Cat2    Cat3
1     NaN     75      NaN
2     61      NaN     84
3     NaN     NaN     NaN


ID    Cat1    Cat2    Cat3
1     54      NaN     44
2     NaN     38     NaN
3     49      50      53

I want to merge them into one DataFrame while keeping the same Column Names. So the result would look like this:

ID    Cat1    Cat2    Cat3
1     54      75      44
2     61      38      84
3     49      50      53

I tried:

df3 = pd.merge(df1, df2, on='ID', how='outer')

Which gave me a DataFrame containing twice as many columns. How can I merge the values from each DataFrame into one?

解决方案

You probably want df.update. See the documentation.

df1.update(df2, raise_conflict=True)

这篇关于 pandas - 合并两个具有相同列名称的DataFrames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆