[英]How to find matching values in 3 columns of 2 different dataframes in pandas and perform an action when the condition is true
I have 2 dataframes.我有 2 个数据框。 Df1 looks like this Df1 看起来像这样
df2 looks like this df2 看起来像这样
i want to compare three columns in both these dataframes, namely, Application_ID, Task Type and Task Category.我想比较这两个数据框中的三列,即 Application_ID、任务类型和任务类别。 If there is a row where these 3 column values match (in the screenshots above, these column values do match), I want to create a column called Task_ID in df1 and assign it to the value of Task_ID in df2.如果这 3 列值匹配的行(在上面的屏幕截图中,这些列值确实匹配),我想在 df1 中创建一个名为 Task_ID 的列并将其分配给 df2 中的 Task_ID 值。
In other words, if there is a match, Task_ID for df1 = 1234 (since the Task_ID for df2 is 1234).换句话说,如果匹配,df1 的 Task_ID = 1234(因为 df2 的 Task_ID 是 1234)。 How do I do this?我该怎么做呢? Any help is most welcome.欢迎任何帮助。 thanks in advance.提前致谢。
I did not test it, as I don't have a sample dataset from you, however here is my solution using pd.merge :我没有测试它,因为我没有您提供的示例数据集,但是这是我使用pd.merge的解决方案:
pd.merge(df1, df2[['Application_ID', 'Task Type', 'Task Category', 'Task_ID']],
on=['Application_ID', 'Task Type', 'Task Category'], how='left')
Hope it works!希望它有效!
Try something like this:尝试这样的事情:
df1 = pd.DataFrame({
'Overal PIA Status': ['In Progress'],
'Task Type': ['Privacy Monitoring'],
'Task Category': ['PIA Monitoring'],
'Due Date': ['9/30/2022'],
'Custodian': ['asdfghjkl'],
'Application_ID': [1234]
})
df2 = pd.DataFrame({
'Task Type': ['Privacy Monitoring'],
'Task Category': ['PIA Monitoring'],
'Task Title': ['Application PIA Not Started'],
'Due Date': ['9/24/2022'],
'Task Owner': ['asdfghjkl'],
'Application_ID': [1234],
'Task_ID': [5678]
})
df1['Task_ID'] = [
df2['Task_ID'][i]
if set(df2[['Application_ID', 'Task Type', 'Task Category']].iloc[i])
== set(df2[['Application_ID', 'Task Type', 'Task Category']].iloc[i])
else None
for i in range(len(df1))
]
print(df1)
Output: Output:
Overal PIA Status Task Type Task Category Due Date Custodian Application_ID Task_ID
0 In Progress Privacy Monitoring PIA Monitoring 9/30/2022 asdfghjkl 1234 5678
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.