简体   繁体   中英

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?

我相信你需要参数fill_value=0

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM