pythonpandasdataframe

Performing mathematical operations on a pandas dataframe


The column looks like

 Mod_month  Mod_year    Reg_Year    Reg_Month  
 10          2016       2016         10
 1           2018       2016         12
 2           2017       2017         2

I want to perform some mathematical operations on columns of a dataframe to calculate difference between dates.

I've tried using:

df['difference']=df[df['mod_month']-df['last_month']+df['mod_month']*12-df['last_year']]

But I get this error:

KeyError: '[-1896 -2015 -1993 ... -1955 -1877 -1981] not in index'

Which I think is due to null values, I also tried using coerce = 'True', which returns invalid syntax.

I have seen other posts, but none of them has the error that I have, therefore any help would be appreciated.


Solution

  • I think need remove df[], because it is syntax of boolean indexing or selecting by subset of columns:

    df['difference'] = df['mod_month'] - df['last_month'] + df['mod_month'] * 12 - df['last_year']