Streaks Of True Or False In Pandas Series
I'm trying to work out how to show streaks of True or False in a pandas Series. Data: p = pd.Series([True,False,True,True,True,True,False,False,True]) 0 True 1 False 2
Solution 1:
You can use cumcount
of consecutives groups created by compare if p
is not equal with shift
ed p
and cumsum
:
print (p.ne(p.shift()))
0True1True2True3False4False5False6True7False8True
dtype: boolprint (p.ne(p.shift()).cumsum())
011223334353647485
dtype: int32
print (p.groupby(p.ne(p.shift()).cumsum()).cumcount())
001020314253607180
dtype: int64
Thank you MaxU for another solution:
print (p.groupby(p.diff().cumsum()).cumcount())
001020314253607180
dtype: int64
Solution 2:
Another alternative solution is create the cumulative sum of p
Series
and subtract the most recent cumulative sum where p
is 0
. Then invert p
and do same. Last multiple Series
together:
c = p.cumsum()
a = c.sub(c.mask(p).ffill(), fill_value=0).sub(1).abs()
c = (~p).cumsum()
d = c.sub(c.mask(~(p)).ffill(), fill_value=0).sub(1).abs()
print (a)
00.011.020.031.042.053.061.071.080.0
dtype: float64
print (d)
01.010.021.031.041.051.060.071.081.0
dtype: float64
print (a.mul(d).astype(int))
001020314253607180
dtype: int32
Post a Comment for "Streaks Of True Or False In Pandas Series"