简体   繁体   English

逐行比较两个单独的熊猫数据帧并返回匹配值

[英]compare two seperate pandas dataframes row by row and return matching values

I have two pandas data frames df1 and df2.我有两个熊猫数据框 df1 和 df2。 df1 contains 2 columns and 750 rows, df2 has 2 columns and 88 rows. df1 包含 2 列和 750 行,df2 包含 2 列和 88 行。 I want to compare the two data frames and return the values from df1 that are present in df2 and store the matching values in a new column in df2.我想比较两个数据框并返回 df1 中存在于 df2 中的值,并将匹配值存储在 df2 中的新列中。 Ex.前任。

df1

    A            B
    emp_table    emp_id
    emp_table    emp_name 
    pay_table    basic_amount
    pay_table    da_amount

df2
A              B
emp_table      emp_id
emp_table      emp_department
pay_table      da_amount

I want to add another column in df2 which has the matching values.我想在 df2 中添加具有匹配值的另一列。

df2
A           B
emp_table   emp_id
pay_table   da_amount

I want to perform one to many comparison of each element of df1 with each element of df2.我想对 df1 的每个元素与 df2 的每个元素进行一对多比较。

I think you need merge without parameter on , so all columns are joined:我认为您需要在没有参数的情况下merge on ,因此所有列都已加入:

df = pd.merge(df1, df2)
print (df)
           A          B
0  emp_table     emp_id
1  pay_table  da_amount

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM