Skip to content
Advertisement

How to perform addition of two dataframe columns based on condition

I have two dataframes with different sizes and I am trying to perform an addition of values from a common column found in both dataframes based on matching values in another common column. My first dataframe looks like this:

df1

JavaScript

df2 looks like this

JavaScript

My expected results is

JavaScript

How would I go about this please? My first thought was to iterate over every line but then I read that this is discouraged as it has poor performance.

Advertisement

Answer

You can use map + set_index + fillna:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement