Search code examples
pythonpandasdifferencesubtraction

difference between two rows pandas


i have a dataframe as :

id|amount|date
20|-7|2017:12:25
20|-170|2017:12:26
20|7|2017:12:27

i want to subtract each row from another for 'amount' column:

the output should be like:

id|amount|date|amount_diff
20|-7|2017:12:25|0
20|-170|2017:12:26|-177
20|7|2017:12:27|-163

i used the code:

df.sort_values(by='date',inplace=True)

df['amount_diff'] = df['invoice_amount'].diff()

and obtained the output as:

id|amount|date|amount_diff
20|-7|2017:12:25|163
20|-170|2017:12:26|-218
20|48|2017:12:27|0

Solution

  • IIUC you need:

    df.sort_values(by='date',inplace=True)
    df['amount_diff'] = df['amount'].add(df['amount'].shift()).fillna(0)
    print (df)
       id  amount        date  amount_diff
    0  20      -7  2017:12:25          0.0
    1  20    -170  2017:12:26       -177.0
    2  20       7  2017:12:27       -163.0
    

    Because if want subtract your solution should work:

    df.sort_values(by='date',inplace=True)
    df['amount_diff1'] = df['amount'].sub(df['amount'].shift()).fillna(0)
    df['amount_diff2'] = df['amount'].diff().fillna(0)
    print (df)
       id  amount        date  amount_diff1  amount_diff2
    0  20      -7  2017:12:25           0.0           0.0
    1  20    -170  2017:12:26        -163.0        -163.0
    2  20       7  2017:12:27         177.0         177.0