First-difference certain columns in generalised code

Viewed 61
df   fruit   date              price   cost         var1   
0     apple    2017-01-01         2      2          20    
1     apple    2017-01-02         3      3          40 
2    banana    2017-01- 02        4      4          20 
2    banana    2017-01-02         4      4          10

Is there a way to generate a first-difference dataframe (fd_df) that keeps fruit,var1,date from df but first-differences all remaining variables by fruit? I have 20 columns to first-difference (including price and cost) so need generalised code.

1 Answers

IIUC, you can set_index the column you want to keep as it is and use groupby.diff on fruit, then reset_index to get the index back as columns.

df_ = (df.set_index(['fruit','date','var1'])
         .groupby(level='fruit').diff()
         .reset_index()
      )
print(df_)
    fruit        date  var1  price  cost
0   apple  2017-01-01    20    NaN   NaN
1   apple  2017-01-02    40    1.0   1.0
2  banana  2017-01-02    20    NaN   NaN
3  banana  2017-01-02    10    0.0   0.0
Related