Issue
I have a dataframe like this:
import pandas as pd
df = pd.DataFrame({'group': [1, 1, 1, 2, 2, 3, 3, 3, 3], 'time': [12, 44, 55, 2, 7, 100, 105, 106, 200]})
# group time
# 0 1 12
# 1 1 44
# 2 1 55
# 3 2 2
# 4 2 7
# 5 3 100
# 6 3 105
# 7 3 106
# 8 3 200
And I want to get something like:
# group time diff_to_next_step
# 0 1 12 32 (44-12)
# 1 1 44 11 (55-44)
# 2 1 55 None
# 3 2 2 5 (7-2)
# 4 2 7 None
# 5 3 100 5 (105 - 100)
# 6 3 105 1 (106 - 105)
# 7 3 106 94 (200 - 106)
# 8 3 200 None
I think this should somehow work with group / stack / shift, but I don't get it to work at the moment. Can somebody please help me?
Solution
Try with shift
df['new'] = df.groupby('group')['time'].shift(-1) - df.time
Answered By - BENY
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.