简体   繁体   English

如何基于一个数据框中的一列和第二个数据框中的两列合并两个数据框

[英]How to merge two data frames based on one column in one data frame and two column in second dataframe

I have two data frames:我有两个数据框:

df1 = pd.DataFrame({'A': ['A1', 'A2', 'A3','A4','A5']})

df2 = pd.DataFrame({'AA': ['A1', 'A3','A16','A1'],
                      'BB': ['A2', 'Z1','A12','X9'],
                      'CC': ['121', '345','444','432'],
                      'DD': ['D0', 'D1','783','980']})

df1: df1:

    A
0   A1
1   A2
2   A3
3   A4
4   A5

df2: df2:

    AA  BB  CC  DD
0   A1  A2  121 D0
1   A3  Z1  345 D1
2   A16 A12 444 783
3   A1  X9  432 980

I want to merge these two data frames based on the condition that the resulting data frame consists of those rows of DF2 whose values in either column AA or column BB matches with values in column A in DF1.我想根据生成的数据框由 DF2 的那些行组成的条件合并这两个数据框,这些行 AA 列或 BB 列中的值与 DF1 中的 A 列中的值匹配。

For example:例如:

    AA  BB  CC  DD
0   A1  A2  121 D0
1   A1  A2  121 D0
2   A1  X9  432 980
1   A3  Z1  345 D1

I tried it the following way, but I am not sure if it is the right approach.我按以下方式尝试过,但我不确定这是否是正确的方法。 First I merged on column A(df1) and AA(df2):首先我在列 A(df1) 和 AA(df2) 上合并:

half1 = pd.merge(df1, df2,  how='left', left_on=['A'], right_on = ['AA'])

Then I merged on column A(df1) and BB(df2):然后我在列 A(df1) 和 BB(df2) 上合并:

half2 = pd.merge(df1, df2,  how='left', left_on=['A'], right_on = ['BB'])

and then took union:然后采取联合:

union = half1.merge(half2, how='outer')

but the result is not what I want.但结果不是我想要的。

For your data working test membership for both columns by Series.isin with filter by boolean indexing and then concat :对于Series.isin两个列的数据工作测试成员资格,通过boolean indexing过滤然后concat

df3 = pd.concat([df2[df2['AA'].isin(df1['A'])],
                 df2[df2['BB'].isin(df1['A'])]]).sort_index()
print (df3)
   AA  BB   CC   DD
0  A1  A2  121   D0
0  A1  A2  121   D0
1  A3  Z1  345   D1
3  A1  X9  432  980

Your solution should be changed with inner join with rename columns and also pd.concat :您的解决方案应该使用rename列的内部连接以及pd.concat

half1 = pd.merge(df1.rename(columns={'A':'AA'}), df2, on='AA')
half2 = pd.merge(df1.rename(columns={'A':'BB'}), df2, on='BB')

df3 = pd.concat([half1,half2]).sort_index()
print (df3)
   AA  BB   CC   DD
0  A1  A2  121   D0
0  A1  A2  121   D0
1  A1  X9  432  980
2  A3  Z1  345   D1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过将第一个数据帧的一列与第二个数据帧的两列匹配来合并两个数据帧 - Merge two data frames by matching one column from the first data frame with two columns from the second data frame 连接两个数据帧,dataframe 中的一列具有多值数据 - Join two data frames, with one column in a dataframe having multivalue data 如何根据一列中的相似值水平合并具有相同列名的两个数据框 - How to merge two data frames having same column names horizontally on basis of similar values in one column 如何根据pandas python中的特定列合并两个数据框? - how to merge two data frames based on particular column in pandas python? 如何根据 Python Pandas 中第二个数据帧中的几列合并两个数据帧? - How to merge two Data Frames based on a few columns in second Data Frame in Python Pandas? 遍历两个数据帧,并用 pandas 中的第二个数据帧的一列更新第一个数据帧的一列 - Iterate through two data frames and update a column of the first data frame with a column of the second data frame in pandas 如果来自一列的数据存在于另一列中,则合并两个数据框 - Merge two data Frame if data from one column is present in another column Python Pandas:比较一列中的两个数据帧,并返回另一个数据帧中两个数据帧的行内容 - Python Pandas : compare two data-frames along one column and return content of rows of both data frames in another data frame 合并两个数据框,只有一列不同。 需要在新数据框中附加该列。 请查看下面的详细视图 - Merge two data-frames with only one column different. Need to append that column in the new dataframe. Please check below for detailed view 如何比较两个数据帧并根据特定条件更改一个数据帧中的值 - How to compare the two data frames and change the value in one data frame based on specific condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM