I have a pandas dataframe looking like.
time value group
0 1 12 1
1 2 14 1
2 3 15 2
3 4 15 1
4 5 18 2
5 6 20 1
6 7 19 2
7 8 24 2
I know want to calculate the spread between group 1 and group 2 for the latest values. I.e. in each row I want to look at the latest value for group 1 and group 2 and calculate value of group 1 - value of group 2.
In the example the output should look like
time value group diff
0 1 12 1 0
1 2 14 1 0
2 3 15 2 -1
3 4 15 1 0
4 5 18 2 -3
5 6 20 1 2
6 7 19 2 1
7 8 24 2 -4
The only function I could find so far was pd.diff() but it doesn't satisfy my needs. So I would really appreciate some help here. Thanks!