Search code examples
pythonpandasdataframesubtraction

Is there a way to subtract the values of a column with another shifted column in pandas?


My data looks like this:

       Coln_A   Coln_B
----------------------
Row_1      12    22000
Row_2      40    25000
Row_3      34    27000
Row_4      21    35000
Row_5      32    53000
Row_6      41    36000
Row_7      17    64000

I am trying to subtract the values of 'shifted 2 rows down Coln_B' with 'Coln_A'.

import pandas as pd

cars = {'Coln_A': [12,40,34,21,32,41,17],
        'Coln_B': [22000,25000,27000,35000,53000,36000,64000]
        }
df = pd.DataFrame(cars, columns = ['Coln_A','Coln_B'], 
                  index=['Row_1','Row_2','Row_3','Row_4','Row_5','Row_6','Row_7'])

# Shifting Coln_B 2 rows down
df['Coln_B'] = df['Coln_B'].shift(2)

# Subtracting Coln_B with Coln_A
df['New']= df['Coln_B']- df[ 'Coln_A']

print (df)

Here is the output and it works. But is there a better/shorter way to solve this with pandas ?

       Coln_A    Coln_B      New

Row_1     12        NaN      NaN
Row_2     40        NaN      NaN
Row_3     34    22000.0  21966.0
Row_4     21    25000.0  24979.0
Row_5     32    27000.0  26968.0
Row_6     41    35000.0  34959.0
Row_7     17    53000.0  52983.0


Solution

  • You can use assign. But this is just synctactic sugar for doing what you already do (and that is likely the most efficient way):

    df.assign(Coln_B=df['Coln_B'].shift(2),
              New=df['Coln_B'].shift(2)-df['Coln_A']
             )
    

    output:

           Coln_A   Coln_B      New
    Row_1      12      NaN      NaN
    Row_2      40      NaN      NaN
    Row_3      34  22000.0  21966.0
    Row_4      21  25000.0  24979.0
    Row_5      32  27000.0  26968.0
    Row_6      41  35000.0  34959.0
    Row_7      17  53000.0  52983.0