Home > Software design >  Substract multiple columns of dataframe based on condition with three columns in Python
Substract multiple columns of dataframe based on condition with three columns in Python

Time:07-10

I have two dataframes, A and B. Each has the same dimensions and same columns. Lets say I want to substract both dataframes (dfB-dfA) if the values of the rows in the first three columns match. Below is an example.

dfA

enter image description here

dfB

enter image description here

NewDF

enter image description here

In the example above, it is easy because the columns are organised, however, in reality both dataframes are much bigger.

I tried with a for loop but it didn't work. I would much appreciate your help.

Best regards.

CodePudding user response:

Pandas is smart enough to take the indices into account. Just convert the columns you want to match into the index and perform your operation as NewDF = dfA-dfB

  • Related