Reputation: 564
How merge or join two dataframes, but keeping certain columns of both?
I need to merge this two dataframes into one. dataframe 2 has all the columns dataframe 1 just need the column "leads"
Dataframe1
campaignid leads
35119190 391
31664745 365
4899110 211
325772660 195
64002140 131
143679198 58
283494007 45
Dataframe2
campaignid cost time reach
35119190 391 391 391
31664745 365 391 391
4899110 211 391 391
325772660 195 391 391
64002140 131 391 391
143679198 58 391 391
283494007 45 391 391
Desired result:
Dataframe2
campaignid cost time reach leads
35119190 391 391 391 391
31664745 365 391 391 365
4899110 211 391 391 211
325772660 195 391 391 195
64002140 131 391 391 131
143679198 58 391 391 58
283494007 45 391 391 45
g_spend.to_dict()
{'id': {0: 35119190,
1: 64002140,
2: 272351300,
3: 4899110,},
'Campaign_ID_name': {0: 'brand',
1: '-',
2: '-',
3: 'science',
,
'Month': {0: '2019|08',
1: '2019|08',
2: '2019|08',
3: '2019|08',
},
'Account': {0: 'a',
1: 'a',
2: 'b',
3: 'c',
},
'campaignid': {0: 35119190,
1: 64002140,
2: 272351300,
3: 4899110,
},
'campaign_name': {0: 'All_Brand',
1: 'All',
2: 'All_GBHS',
3: 'All_Science',
},
'cost': {0: '$59,399.37 ',
1: '$12,660.37 ',
2: '$5,631.96 ',
}}
grouped_cw.to_dict()
{'leads': {'1076533154': 40.0,
'143679198': 58.0,
'169278078': 13.0,
'1729099155': 8.0,
}}
Upvotes: 1
Views: 160
Reputation: 153460
Let's use map
:
df2['leads'] = df2['campaignid'].map(df1.set_index('campaignid')['leads'])
df2
Output:
campaignid cost time reach leads
0 35119190 391 391 391 391
1 31664745 365 391 391 365
2 4899110 211 391 391 211
3 325772660 195 391 391 195
4 64002140 131 391 391 131
5 143679198 58 391 391 58
6 283494007 45 391 391 45
Try
df2['leads'] = df2['campaignid'].map(grouped_cw)
Upvotes: 1