I have a pandas data frame of the form
index | value | condition |
---|---|---|
0 | 11 | False |
1 | 12 | True |
2 | 13 | True |
3 | 14 | False |
4 | 15 | True |
5 | 16 | True |
6 | 17 | True |
7 | 18 | False |
My goal is to get all differences of the value of consecutive groups of rows satisfying the condition, i.e. in this example, I want to have a list [13 - 12, 17 - 15]
. Is there an efficient way with pandas to do this?
You can take cumsum
on condition
column to identify the blocks
of rows satisfying the condition, then group
the column value
on these blocks
and agg
using numpy.ptp
:
m = df['condition']
blocks = (~m).cumsum()[m]
lst = list(df['value'].groupby(blocks).agg(np.ptp))
Details:
>>> blocks
1 1
2 1
4 2
5 2
6 2
Name: condition, dtype: int64
>>> lst
[1, 2]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments