Python 使用特定列连接两个 Pandas 数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30584486/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:39:53  来源:igfitidea点击:

join two pandas dataframe using a specific column

pythonjoinpandasdataframe

提问by ahajib

I am new with pandas and I am trying to join two dataframes based on the equality of one specific column. For example suppose that I have the followings:

我是熊猫的新手,我试图根据一个特定列的相等性连接两个数据框。例如,假设我有以下内容:

df1
A    B    C
1    2    3
2    2    2

df2
A    B    C
5    6    7
2    8    9

Both dataframes have the same columns and the value of only one column (say A) might be equal. What I want as output is this:

两个数据框具有相同的列,并且只有一列(例如 A)的值可能相等。我想要的输出是这样的:

df3
A    B    C   B    C
2    8    9   2    2

The values for column 'A' are unique in both dataframes.

列“A”的值在两个数据框中都是唯一的。

Thanks

谢谢

采纳答案by vk1011

pd.concat([df1.set_index('A'),df2.set_index('A')], axis=1, join='inner')

If you wish to maintain column Aas a non-index, then:

如果您希望将列维护A为非索引,则:

pd.concat([df1.set_index('A'),df2.set_index('A')], axis=1, join='inner').reset_index()

回答by Paul H

Alternatively, you could just do:

或者,你可以这样做:

df3 = df1.merge(df2, on='A', how='inner', suffixes=('_1', '_2'))

And then you can keep track of each value's origin

然后你可以跟踪每个值的来源