2

I have a dataframe like:

SEQ_N FREQ  VAL
ABC   1     121
ABC   1     130
ABC   1     127
ABC   1     116 
DEF   1     345
DEF   1     360
DEF   1     327
DEF   1     309

I want to subtract 1st row value from subsequent rows for each group.

Result:

SEQ_N FREQ  VAL  RES
ABC   1     121   0
ABC   1     130   9
ABC   1     127   6
ABC   1     116  -5
DEF   1     345   0
DEF   1     360   15
DEF   1     327  -18
DEF   1     309  -36
Rohit
  • 435
  • 3
  • 10

1 Answers1

3

Subtract column by Series.sub with transform and first for get first value per groups to Series with same size like original:

df['RES'] = df['VAL'].sub(df.groupby('SEQ_N')['VAL'].transform('first'))
print (df)
  SEQ_N  FREQ  VAL  RES
0   ABC     1  121    0
1   ABC     1  130    9
2   ABC     1  127    6
3   ABC     1  116   -5
4   DEF     1  345    0
5   DEF     1  360   15
6   DEF     1  327  -18
7   DEF     1  309  -36
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • What does transform do here? The link does not say much – urdearboy Mar 03 '19 at 05:31
  • 2
    @urdearboy it transforms the result which you are calling as a series for the groups, here it is the first value. you can test using `print(df.groupby('SEQ_N')['VAL'].transform('first'))` :) – anky Mar 03 '19 at 05:34
  • 2
    @urdearboy -also maybe help [this](https://stackoverflow.com/q/40957932/2901002) answers. – jezrael Mar 03 '19 at 05:34