Reputation:
If Current value is > Previous Value (value.shift(-1)) Then add value to df[''].
Start from bottom to top. Please see my expecting dataframe to see what I'm trying to accomplish
Upvotes: 1
Views: 89
Reputation: 30920
We can DataFrame.sort_values
with groupby.cumsum
checking condition with groupby.diff
.
df2=df.sort_values('timestamp')
blocks=df2.groupby('id')['value'].diff(-1).ge(0).cumsum()
df['finalvalue']=df2['value'].mask(blocks.ne(0),
df2.groupby(['id',blocks])['value'].cumsum())
print(df)
timestamp id value finalvalue
0 2020-02-16 03:05:57 device1 1000 4100
1 2020-02-16 02:05:33 device1 900 3100
2 2020-02-16 01:05:08 device1 700 2200
3 2020-02-16 00:04:44 device1 300 1500
4 2020-02-15 23:04:19 device1 1200 1200
5 2020-02-15 22:03:55 device1 800 800
6 2020-02-15 21:03:29 device1 400 400
7 2020-02-15 20:03:04 device1 200 200
8 2020-02-15 19:02:39 device1 100 100
9 2020-02-15 18:02:14 device1 0 0
Upvotes: 1