If I have the following pandas series:
import pandas as pd
s = pd.Series([0,1,1,0,1,0,0,1,1,0,1,1,1])
I want a list of counts of consecutive number of ones i.e. in this case the required output is
[2,1,2,3]
How can I obtain the above output?
If I have the following pandas series:
import pandas as pd
s = pd.Series([0,1,1,0,1,0,0,1,1,0,1,1,1])
I want a list of counts of consecutive number of ones i.e. in this case the required output is
[2,1,2,3]
How can I obtain the above output?
Let us try use cumsum create the new key
s[s==1].groupby(s.eq(0).cumsum()).sum()
1 2
2 1
4 2
5 3
dtype: int64
#s[s == 1].groupby(s.eq(0).cumsum()).sum().tolist()
#[2, 1, 2, 3]
The following should work:
[len(i) for i in ''.join([str(i) for i in list(s)]).split('0') if '1' in i]
Output:
[2, 1, 2, 3]