简体   繁体   English

Pandas 比较不同数据帧的两列,如果匹配,则复制第三列的值

[英]Pandas compare two columns of different dataframes and copy value of a third column if there is a match

I have two different dataframes that I have exported from different .xlsx documents我有两个不同的数据框,它们是从不同的.xlsx文档中导出的

df1 contains the headers: Contact Name, Request ID, Contact E-mail. df1 包含标题:联系人姓名、请求 ID、联系人电子邮件。
df2 contains the headers: Request ID and some other headers that I need. df2 包含标头:请求 ID 和我需要的其他一些标头。

I'd like to check if the value of df2['Request ID'] matches with each of df1 ['Request ID] column and copy the df1['Contact E-mail'] value of that match to a new column in df2.我想检查 df2['Request ID'] 的值是否与每个 df1 ['Request ID] 列匹配,并将该匹配项的 df1['Contact E-mail'] 值复制到 df2 中的新列.

I have tried many things but without success, the last thing that I tried was:我尝试了很多事情但没有成功,我尝试的最后一件事是:

 if df1.loc['Request ID'] == df2.loc['Request ID']:
        df2['Email'] = df1['Contact Email']
 else:
        df2['Email'] = ""

However I'm getting error:但是我收到错误:

KeyError: "None of [Int64Index( KeyError:“ [Int64Index(
[2014164503, 2014151100, 2014149017, 2014162644, 2014164489, [2014164503、2014151100、2014149017、2014162644、2014164489、
2014120882, 2014159262, 2013993561, 2014162450, 2014151098, 2014120882、2014159262、2013993561、2014162450、2014151098、
2014150239, 2014162949, 2014151485, 2014162448, 2014163094, 2014150239、2014162949、2014151485、2014162448、2014163094、
2014162592, 2014158897, 2014151097, 2014164495, 2014159459, 2014162592、2014158897、2014151097、2014164495、2014159459、
2014036452, 2014164484, 2014154555, 2014154556, 2014146835, 2014036452、2014164484、2014154555、2014154556、2014146835、
2014149163, 2014158899, 2014153930, 2014163579, 2014036453, 2014149163、2014158899、2014153930、2014163579、2014036453、
2014142169, 2014145026, 2014151724, 2014155704, 2014152046, 2014142169、2014145026、2014151724、2014155704、2014152046、
2014036447, 2014164490, 2014163503, 2014160983, 2014105630, 2014036447、2014164490、2014163503、2014160983、2014105630、
2014163612, 2014146834, 2014027641, 2014164454], 2014163612, 2014146834, 2014027641, 2014164454],
dtype='int64')] are in the [index]" dtype='int64')] 在 [index]"

What you are trying to achieve sounds like a typical sql join operation.您要实现的目标听起来像是典型的sql join操作。 This can be translated to Pandas as this:这可以翻译成Pandas如下所示:

df1 = df1.rename(columns={"Contact Email": "Email"})
res = df1.merge(df2, on=["Request ID"], how="right")

Pandas merge docs Pandas 合并文档

I would be great if you share an example of your input and the expected output.如果您分享您的输入和预期输出的示例,我会很棒。

暂无
暂无

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

相关问题 Pandas 比较两个数据框中的两列,如果有匹配项,则从第三列获取值转换为周数 - Pandas compare two columns from two dataframes if there is a match get value from third column convert to week number Pandas 数据框 - 匹配两个数据框中的两列以更改第三列的值 - Pandas dataframes - Match two columns in the two dataframes to change the value of a third column 如果只有第一个唯一值匹配,Pandas会比较两列并复制另一列的值 - Pandas compare two columns and copy value of another column if there is a match only for first unique value 比较来自两个不同数据框熊猫的列 - Compare columns from two different dataframes pandas 比较来自不同数据框的两个系列,并将找到的匹配项替换为第三个系列值 - Compare two series from different dataframes, and where match found replace with third series value 匹配两个数据框并填充pandas中的列值 - Match two dataframes and fill the column value in pandas pandas:比较来自两个不同大小的不同数据帧的字符串列 - pandas: compare string columns from two different dataframes of different sizes 比较不同pandas数据帧中的列 - Compare columns in different pandas dataframes 比较和匹配 pandas 两个不同数据帧中的时间戳范围 - Compare and match range of timestamps in pandas two different dataframes Dataframe - 对于每一行,比较两列的值,匹配时获取第三列的值 - Dataframe - for each row, compare values of two columns, get value of third column on match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM