I have a dataframe like below:
import pandas as pd
df = pd.DataFrame({"id":[1,1,1,1,1,1,2,2,2,2,3,3,3,3],
"val":[0,1,1,0,1,0,0,1,0,1,0,0,0,1]})
id val
0 1 0
1 1 1
2 1 1
3 1 0
4 1 1
5 1 0
6 2 0
7 2 1
8 2 0
9 2 1
10 3 0
11 3 0
12 3 0
13 3 1
As you can see the ID's are duplicated and I have values alternating between 0 and 1. I'd like to somehow calculate the number of times when values switch from 0 to 1. For example like this:
id val
1 2
2 2
3 1
It's not a count of 1, but rather counter number of times val switch for 0-->1 for each ID.
By using diff
df.groupby('id').val.apply(lambda x : sum(x.diff().eq(1)))
Out[306]:
id
1 2
2 2
3 1
Name: val, dtype: int64
You could do something like:
>>> # To be more exact: (df['val'] == 1) & (df['val'].shift() == 0
>>> df['val'].diff().eq(1).groupby(df['id']).sum().astype(int)
id
1 2
2 2
3 1
Generally, I like to avoid groupby.apply(...)
if at all possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With