I have 2 df, and I want change value in df1, if it has two matching values in df2
campaign_name | adgroup_name | status |
---|---|---|
apple_campaign | apple_1 | paused |
apple_campaign | apple_2 | paused |
apple_campaign | apple_3 | paused |
pear_campaign | pear_1 | paused |
pear_campaign | pear_2 | paused |
pear_campaign | pear_3 | paused |
pear_campaign | pear_4 | paused |
peach_campaign | peach_1 | paused |
peach_campaign | peach_2 | paused |
campaign_name | adgroup_name | headline | others |
---|---|---|---|
apple_campaign | apple_1 | apple chips | etc |
apple_campaign | apple_1 | apple cake | etc |
apple_campaign | apple_3 | apple juice | etc |
pear_campaign | pear_1 | pear chips | etc |
pear_campaign | pear_2 | pear cake | etc |
pear_campaign | pear_4 | pear juice | etc |
pear_campaign | pear_4 | pear pumpkin | etc |
So, I want to set "Status"->enabled, if df2 has the same "campaign_name", and "adgroup_name"
CodePudding user response:
You could merge
with indicator and identify the merged rows:
df1.loc[(df1
.merge(df2, on=['campaign_name', 'adgroup_name'], how='left', indicator=True)
['_merge'].eq('both')
), 'status'] = 'enabled'
output:
campaign_name adgroup_name status
0 apple_campaign apple_1 enabled
1 apple_campaign apple_2 enabled
2 apple_campaign apple_3 paused
3 pear_campaign pear_1 enabled
4 pear_campaign pear_2 enabled
5 pear_campaign pear_3 enabled
6 pear_campaign pear_4 paused
7 peach_campaign peach_1 enabled
8 peach_campaign peach_2 enabled