Skip to content Skip to sidebar Skip to footer

How To Merge 2 Df Based On Comparison Of 2 Columns To Match 1 Column

How to .merge 2 df, 1 column to match 2 columns ?? The goal is to merge 2 df to have count of records for every campaign id from a REF table to the Data by id. The issue .merge ju

Solution 1:

I would first set id_name as index in g_spend, then do a replace on cw, followed by a value_counts:

s = (cw.campaignid
       .replace(g_spend.set_index('id_name').campaignid
       .value_counts()
       .to_frame('leads')
    )

g_spend = g_spend.merge(s, left_on='campaignid', right_index=True)

Output:

campaignidid_namecostleads0154campaign11551155campaign212121566  campaign331233158campaign4332

Post a Comment for "How To Merge 2 Df Based On Comparison Of 2 Columns To Match 1 Column"