Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract two DataFrames with non overlapping indexes

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?

like image 826
synapski Avatar asked Feb 09 '15 23:02

synapski


People also ask

Can you subtract two Dataframes?

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.

Can you subtract two pandas series?

Any python sequence of same length like a Python tuple, a Python list can be subtracted from a pandas.

Can you merge Dataframes on index?

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.


1 Answers

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.

like image 51
Alex Riley Avatar answered Sep 28 '22 08:09

Alex Riley