[英]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")
I would be great if you share an example of your input and the expected output.如果您分享您的输入和预期输出的示例,我会很棒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.