Reputation: 40
I have a column of a pandas DataFrame that looks like this:
1 False
2 False
3 False
4 True
5 True
6 False
7 False
8 False
9 False
10 False
11 True
12 False
I would like to get the count of False between the True. Something like this:
1 3
2 0
3 5
4 1
This is what I've done:
counts = []
count = 0
for k in df['result'].index:
if df['result'].loc[k] == False:
count += 1
else:
counts.append(count)
count = 0
where counts would be the result. Is there a simpler way?
Upvotes: 0
Views: 364
Reputation: 36608
You can use the groupby
function from the itertools
package to group the False and True values together and append the count to a list.
s = pd.Series([False,False,False,True,True,False,False,False,False,False,True,False],
index=range(1,13)
from itertools import groupby
out = []
for v,g in groupby(s):
if not v: # v is false
out.append(len(tuple(g)))
else: # v is true
out.extend([0]*(len(tuple(g))-1))
out
[3, 0, 5, 1]
Upvotes: 0
Reputation: 214927
Group by the cumulative sum of itself and then count the False
with sum
:
s = pd.Series([False, False, False, True, True, False, False, False, False, False, True, False])
(~s).groupby(s.cumsum()).sum()
#0 3.0
#1 0.0
#2 5.0
#3 1.0
#dtype: float64
Upvotes: 4