Skip to content
Advertisement

Pandas modify column values based on another DataFrame

I am trying to add values to a column based on a couple of conditions. Here is the code example:

JavaScript

For each value in the ‘Val’ column of df1, I want to add values from df2, based on the type and whether the original value was positive or negative.

The expected output for this example would be alternate 50 and -50 in df1. The above code does the job, but is too slow to be usable for a large data set. Is there a better way to do this?

Advertisement

Answer

Try adding a Cat column to df1 merge then sum val columns across axis 1 then drop the extra columns:

JavaScript
JavaScript

Add new column with np.where

JavaScript
JavaScript

merge on Type and Cat

JavaScript
JavaScript

sum Val columns:

JavaScript
JavaScript

drop extra columns:

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