简体   繁体   English

如何在 pandas 中的 2 个不同数据帧的 3 列中找到匹配值,并在条件为真时执行操作

[英]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.

相关问题 Python 如何加入/合并 Pandas 数据帧与来自不同数据帧的特定值的匹配列 - Python how to join/merge Pandas dataframes with matching columns of specific values from different dataframes Pandas 通过堆叠列与匹配条件的值来组合数据帧 - Pandas combine dataframes by stacking columns with values on matching condition 如何在2个数据框之间的2列中查找具有相同值但在其他列pandas中具有不同值的行 - How to find row with same value in 2 columns between 2 dataframes but different values in other columns pandas 如何至少在条件为真时执行一次循环操作 - How to perform a loop action at least once and also when a condition is true Pandas 在匹配列中加入具有不同间隔的数据帧 - Pandas joining dataframes with different intervals in matching columns 根据列中的值匹配两个 Pandas DataFrame - Matching Two Pandas DataFrames based on values in columns 比较两个不同DataFrame中不同长度的两列的值,如果匹配条件则执行数学运算 - Compare the values of two columns of different length in two different DataFrames and perform a math operation if matches a condition 如何在python pandas的循环中对数据帧执行操作 - How to perform action on dataframes in loop in python pandas 如何在两个数据帧的列之间找到匹配值? - How to find matching values between the columns of two dataframes? 如何在熊猫python中找到具有完全相同的列和索引但值不同的数据框列表的交集? - How to find an intersection of a list of dataframes with exactly same columns and indexes but different values in pandas python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM