Search code examples
pythonpandasdataframesubtraction

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?


Solution

  • 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.