0

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?

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is quite readable and I like it. It is not the fastest according to %timeit but it is not relevant I guess.
1

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

1 Comment

Thanks for the answer. I works like a charm. It is similar to @jezrael approach but more readable in my point of view.
1

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

1 Comment

This approach is the most performant according to %timeit although for me the usage of fill_value is not self-explanatory. Thanks a lot!
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.