Home > Enterprise >  Substract pandas Dataframes with different indices
Substract pandas Dataframes with different indices

Time:12-18

I have two pandas dataframes which I would like to substract. The base dataframe minuend has more indices and columns than the other dataframe subtrahend.

They are defined as following:

minuend = pd.DataFrame(
    {"angles": [0, 3, 4], "degrees": [360, 180, 360]},
    index=["circle", "triangle", "rectangle"],
)

           angles  degrees
circle          0      360
triangle        3      180
rectangle       4      360
subtrahend = pd.Series(
    [1, 1], index=["circle", "triangle"], name="angles"
).to_frame()

          angles
circle         1
triangle       1

The expected result is:

           angles  degrees
circle         -1      360
triangle        3      180
rectangle       3      360

What is the best way to achive this since minuend - subtrahend or minuend.sub(subtrahend_df) does not work out of the box?

CodePudding user response:

One way using pandas.DataFrame.update:

minuend.update(minuend.sub(subtrahend))

Output:

           angles  degrees
circle       -1.0      360
triangle      2.0      180
rectangle     4.0      360

CodePudding user response:

Use df.subtract with df.fillna:

In [1396]: minuend.subtract(subtrahend).fillna(minuend).astype(int)
Out[1396]: 
           angles  degrees
circle         -1      360
rectangle       4      360
triangle        2      180

CodePudding user response:

Use fill_value=0 parameter in DataFrame.sub:

df = minuend.sub(subtrahend, fill_value=0).astype(int)
print (df)
           angles  degrees
circle         -1      360
rectangle       4      360
triangle        2      180

CodePudding user response:

A solution I came up with is:

minuend.loc[subtrahend.index, subtrahend.columns] -= subtrahend

But I am not sure whether this is the most pythonic and efficient way to solve it.

What do you think?

  • Related