pandas 合并数据框保留所有项目熊猫
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42940507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merging dataframes keeping all items pandas
提问by johnnyb
How can I merge two different dataframes, keeping all rows from each dataframe while filling in the blanks?
如何合并两个不同的数据帧,在填充空白时保留每个数据帧的所有行?
DF1
DF1
Name Addr Num Parent Parent_Addr
Matt 123H 8 James 543F
Adam 213H 9 James 543F
James 321H 10 Mom 654F
Andrew 512F 10 Dad 665F
Faith 555A 7 None 657F
DF2
DF2
Name Parent Parent_Num Parent_Addr
Matt James 10 543F
Adam James 10 543F
James Mom 12 654F
None Ian 13 656F
None None None 1234
Expected output
预期输出
Name Addr Num Parent Parent_Num Parent_Addr
Matt 123H 8 James 10 543F
Adam 213H 9 James 10 543F
James 321H 10 Mom 12 654F
Andrew 512F 10 Dad None 665F
Faith 555A 7 None None 657F
None None None Ian 13 656F
None None None None None 1234
I am attempting to merge and keep all data from both dataframes. Any help would be greatly appreciated. THank you.
我正在尝试合并并保留两个数据帧中的所有数据。任何帮助将不胜感激。谢谢你。
回答by Vaishali
You need to merge on all the common columns and use outer join
您需要合并所有公共列并使用外连接
pd.merge(df1, df2, on = ['Name', 'Parent', 'Parent_Addr'], how = 'outer')
Name Addr Num Parent Parent_Addr Parent_Num
0 Matt 123H 8 James 543F 10
1 Adam 213H 9 James 543F 10
2 James 321H 10 Mom 654F 12
3 Andrew 512F 10 Dad 665F NaN
4 Faith 555A 7 None 657F NaN
5 None NaN NaN Ian 656F 13
6 None NaN NaN None 1234 None
回答by piRSquared
You can keep all the rows with an 'outer'
merge
notethat by default merge
will join on all common column names.
您可以使用'outer'
merge
注释保留所有行,默认情况下merge
将连接所有常见列名称。
df1.merge(df2, 'outer')
Name Addr Num Parent Parent_Addr Parent_Num
0 Matt 123H 8.0 James 543F 10
1 Adam 213H 9.0 James 543F 10
2 James 321H 10.0 Mom 654F 12
3 Andrew 512F 10.0 Dad 665F NaN
4 Faith 555A 7.0 None 657F NaN
5 None NaN NaN Ian 656F 13
6 None NaN NaN None 1234 None