Skip to content Skip to sidebar Skip to footer

Adding Columns After Comparing Values In 2 Dataframes With Different Lengths

I referenced another stackoverflow, but the value came out weird and I asked again. like compare 2 columns in different dataframes df1 Name date A 2019-01-24 A 2019-02-14

Solution 1:

You can filter only necessary columns in df2 by list and instead left_on and right_on is used rename with on parameter for avoid same columns date and date2 in output and also is used left join:

df = df1.merge(df2[['Name','date2', 'value']].rename(columns={'date2':'date'), 
               on = ['Name','date'], 
               how='left')

First solution with left join:

df1['new'] = df1.merge(df2,
                       left_on=['Name','date'],
                       right_on=['Name','date2'],
                       how='left')['value']

I think reason why weird output should be created default index after merge with inner join, so if assign back rows should not match. If use left join then index of left DataFrame in not changed, so assign column working correct.

Post a Comment for "Adding Columns After Comparing Values In 2 Dataframes With Different Lengths"