-1

I want to group by the id column in this dataframe:

   id  a  b  c
0   1  1  6  2
1   1  2  5  2
2   2  3  4  2
3   2  4  3  2
4   3  5  2  2
5   3  6  1  2

and add the differences between rows for the same column and group as additional columns to end up with this dataframe:

   id  a  b  c  a_diff  b_diff  c_diff
0   1  1  6  2    -1.0     1.0     0.0
1   1  2  5  2     1.0    -1.0     0.0
2   2  3  4  2    -1.0     1.0     0.0
3   2  4  3  2     1.0    -1.0     0.0
4   3  5  2  2    -1.0     1.0     0.0
5   3  6  1  2     1.0    -1.0     0.0

data here

df = pd.DataFrame({'id': [1,1,2,2,3,3], 'a': [1,2,3,4,5,6],'b': [6,5,4,3,2,1], 'c': [2,2,2,2,2,2]})
BeRT2me
  • 12,699
  • 2
  • 13
  • 31
JFG123
  • 577
  • 5
  • 13
  • 1
    Please post the data in a way that others can use it. It's not viable to ask others to re-type your data from an image. See: [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – BeRT2me Jul 04 '22 at 03:44
  • @BeRT2me amended – JFG123 Jul 04 '22 at 04:09
  • What have you tried? https://stackoverflow.com/help/minimal-reproducible-example – Utpal Kumar Jul 04 '22 at 04:23

1 Answers1

0

Your desired output doesn't make much sense, but I can force it there with:

df[['a_diff', 'b_diff', 'c_diff']] = df.groupby('id').transform(lambda x: x.diff(1).fillna(x.diff(-1)))

Output:

   id  a  b  c  a_diff  b_diff  c_diff
0   1  1  6  2    -1.0     1.0     0.0
1   1  2  5  2     1.0    -1.0     0.0
2   2  3  4  2    -1.0     1.0     0.0
3   2  4  3  2     1.0    -1.0     0.0
4   3  5  2  2    -1.0     1.0     0.0
5   3  6  1  2     1.0    -1.0     0.0
BeRT2me
  • 12,699
  • 2
  • 13
  • 31