2021年3月10日星期三

subtracting rows using diff()

I have a dataframe similar to this:

name  group val  cici   a    3  john   b    2  john   a    1  john   c    5  ian    a    2  ian    a    3  

I am trying to 1) group by name 2) calculate the difference among vals. the returned column should be:

name  group val    delta      cici   a    3   0      john   b    2   0      john   a    1   -1       john   c    5   3      ian    a    2   0      ian    a    3   1  

I used diff() to calculate this, however, for john, I am trying to get b-b, a-b, c-b, but when I use diff(), I got b-b, a-b, c-a...is there anyway I could use the diff to count their difference with the first row in the group? my code:

df.groupby('name')['val'].transform('diff')  

Anyway to fix this?

https://stackoverflow.com/questions/66575891/subtracting-rows-using-diff March 11, 2021 at 11:07AM

没有评论:

发表评论