I'm trying to subtract two DataFrames together. I would like to treat missing values as 0. fillna()
won't work here because I don't know the common indexes before doing the subtraction:
import pandas as pd
A = pd.DataFrame([1,2], index=['a','b'])
B = pd.DataFrame([3,4], index=['a','c'])
A - B
0
a -2
b NaN
c NaN
Ideally, I would like to have:
A - B
0
a -2
b 2
c -4
Is it possible to get that while keeping the code simple?
subtract() function is used for finding the subtraction of dataframe and other, element-wise. This function is essentially same as doing dataframe – other but with a support to substitute for missing data in one of the inputs.
Any python sequence of same length like a Python tuple, a Python list can be subtracted from a pandas.
Merging Dataframes by index of both the dataframes As both the dataframe contains similar IDs on the index. So, to merge the dataframe on indices pass the left_index & right_index arguments as True i.e. Both the dataframes are merged on index using default Inner Join.
You can use the subtract
method and specify a fill_value
of zero:
A.subtract(B, fill_value=0)
Note: the method below, combineAdd
, is deprecated from 0.17.0 onwards.
One way is to use the combineAdd
method to add -B
to A
:
>>> A.combineAdd(-B)
0
a -2
b 2
c -4
With this method, the two DataFrames are added and the values at non-matching indices default to the value in either A
or B
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With