Search code examples
pythonpandasdataframesubtraction

Python pandas subtraction calculation of columns with criteria


I am trying to subtract one column in a data frame from another column in the same data frame. However, I need some additional criteria. For example if both columns are NaNs then I want the result to be NaN. If column 'Feb' is Nan but the corresponding 'Jan'is a number then I want to turn 'Feb'into a zero for the calculation and thus the result just to be the full value of 'Jan' rather than a NaN due to calculation error of value - Nan.

For completeness some additional information: Jan is always higher than Feb. So if there is a Feb number there is always a higher Jan number and in that instance Jan is never a NaN.

Below is some simple same data to try and illustrate.

import pandas as pd


sales = [{'account': 'Jones LLC', 'Jan': 222,  },
         {'account': 'Alpha Co',  'Jan': 240, 'Feb': 50, },
         {'account': 'Delta Co' },
         {'account': 'Blue Inc',  'Jan': 150,  }]
df = pd.DataFrame(sales)

df = df[['account', 'Jan', 'Feb' ]]

df['SubtractionResult'] = df['Jan']-df['Feb']

print (df)

    account    Jan   Feb  SubtractionResult[181]: 
0  Jones LLC  222.0   NaN                NaN
1   Alpha Co  240.0  50.0              190.0
2   Delta Co    NaN   NaN                NaN
3   Blue Inc  150.0   NaN                NaN

I want the result to look like this:

    account    Jan   Feb  SubtractionResult[181]: 
0  Jones LLC  222.0   NaN              222.0
1   Alpha Co  240.0  50.0              190.0
2   Delta Co    NaN   NaN              NaN
3   Blue Inc  150.0   NaN              150.0

Solution

  • You can replace the missing values with zero in Feb column and then do the subtraction:

    df['SubtractionResult'] = df['Jan'] - df['Feb'].fillna(0)
    df
    #     account     Jan    Feb    SubtractionResult
    #0  Jones LLC   222.0    NaN                222.0
    #1   Alpha Co   240.0   50.0                190.0
    #2   Delta Co     NaN    NaN                  NaN
    #3   Blue Inc   150.0    NaN                150.0