pandas python pandas数据框连接两个数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28642177/
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
python pandas dataframe join two dataframes
提问by bjurstrs
I am trying to join to data frames. They look like this
我正在尝试加入数据框。他们看起来像这样
DF1 = ID COUNTRY YEAR V1 V2 V3 V4
12 USA 2012 x y z a
13 USA 2013 x y z a
14 RUSSIA 2012 x y z a
DF2 = ID COUNTRY YEAR TRACT
9 USA 2000 A
13 USA 2013 B
The desired end goal is:
期望的最终目标是:
DF3 = ID COUNTRY YEAR V1 V2 V3 V4 TRACT
9 USA 2000 A
12 USA 2012 x y z a
13 USA 2013 x y z a B
14 RUSSIA 2012 x y z a
I've been trying to use the pd.merge and the .join function with the on='outer' setting to no success
我一直在尝试使用 pd.merge 和 .join 函数与 on='outer' 设置没有成功
df3 = pd.merge(df1,df2,how='outer',left_on=['ID','Country','Year'],right_on=['ID',"Country","Year"])
采纳答案by JAB
try this:
尝试这个:
df.merge(df2,how='outer',left_on=['ID','COUNTRY','YEAR'],right_on=['ID',"COUNTRY","YEAR"])
(the column names should be in caps based on your input tables)
(列名应根据您的输入表大写)
回答by Harvey
Have you tried
你有没有尝试过
df1.join(df2)
You can add parameters later, but it should work.
您可以稍后添加参数,但它应该可以工作。

