kidman01
kidman01

Reputation: 945

How to subtract two DataFrame columns in Pandas

I want to do some basic calculations inside a pandas dataframe, but apparently pandas ignores empty rows. So let's assume my dataframe looks as follows:

ColA ColB
11    6
7    

Then doing df["ColC"] = df["ColA"].subtract(df["ColB"]) will yield

ColA ColB ColC
11    6    5
7         

Whereas I would want that ColC also has a "7" in this case.

What's the best way to do these calculations with DataFrames?

Upvotes: 1

Views: 4017

Answers (1)

jezrael
jezrael

Reputation: 862851

I believe you need parameter fill_value=0:

df["ColC"] = df["ColA"].subtract(df["ColB"], fill_value=0)

Upvotes: 3

Related Questions