pandas 在具有非唯一值的列上合并 Python 中的两个数据框

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

Merge two data frames in Python on column with non-unique values

pythonpandasdataframemerge

提问by tk215

I am trying to merge two dataframes in Python based on column 'X'.

我正在尝试基于列“X”在 Python 中合并两个数据框。

The column X in left dataframe has non-unique values and the column X in right dataframe has unique values. How can I merge the values from the right dataframe into the left dataframe?

左侧数据框中的 X 列具有非唯一值,右侧数据框中的 X 列具有唯一值。如何将右侧数据框中的值合并到左侧数据框中?

I want to merge rows from df2 into df1 to form df3

我想将 df2 中的行合并到 df1 以形成 df3

df1 = pd.DataFrame({'A': ['NA','EU','LA','ME'],
                    'B': [50, 23,21,100],
                    'X': ['IW233', 'IW455', 'IW455', 'IW100']})

df2 = pd.DataFrame({'C': [50, 12, 12, 11, 10, 16],
                    'X': ['IW455', 'IW200', 'IW233', 'IW150', 'IW175', 'IW100'],
                    'D': ['Aug', 'Sep', 'Jan', 'Feb', 'Dec', 'Nov']})

df3:1

df3:1

回答by jezrael

You can use mergewith left join, if only Xis joined column onparameter can be omit:

您可以使用mergeleft join,如果只X连接列on参数可以省略:

df = pd.merge(df1, df2, how='left')
print (df)
    A    B      X   C    D
0  NA   50  IW233  12  Jan
1  EU   23  IW455  50  Aug
2  LA   21  IW455  50  Aug
3  ME  100  IW100  16  Nov

If multiple same columns names:

如果多个相同的列名称:

df = pd.merge(df1, df2, on='X', how='left')
print (df)
    A    B      X   C    D
0  NA   50  IW233  12  Jan
1  EU   23  IW455  50  Aug
2  LA   21  IW455  50  Aug
3  ME  100  IW100  16  Nov

回答by Willem Van Onsem

You can use a joinoperator here:

您可以join在此处使用运算符:

>>> df1.join(df2.set_index('X'),on='X')
    A    B      X   C    D
0  NA   50  IW233  12  Jan
1  EU   23  IW455  50  Aug
2  LA   21  IW455  50  Aug
3  ME  100  IW100  16  Nov

So we first change the index of the right frame to X(since these are unique on the right frame, that is not a problem). Then we perform a join on the Xcolumn.

所以我们首先将右框架的索引改为X(因为这些在右框架上是唯一的,那不是问题)。然后我们对X列执行连接。