Counting rows since condition
Asked Answered
L

2

5

I have a column in Pandas which contains booleans and want to count thr rows since the last True value, something like this:

a           b
False       0
True        0
False       1
False       2 
False       3  
True        0
False       1
True        0

I could do it via a loop but it seems there must be a better way

Lutyens answered 5/9, 2017 at 6:58 Comment(0)
F
8
a = ~df['a']
b = a.cumsum()
c = b-b.where(~a).ffill().fillna(1).astype(int)
print (c)
0    0
1    0
2    1
3    2
4    3
5    0
6    1
7    0
Name: a, dtype: int32
Faultless answered 5/9, 2017 at 7:3 Comment(0)
S
1

I found the above code didn't work, but this does:

#Create DataFrame
import pandas as pd
d = {'condition': [False,False,False,True,False,False,True,False,True,False,False,False,False,False,False,True]}
df = pd.DataFrame(data=d)

#Calculate Rows Since Condition
df['counter'] = df.index.where(df.condition)
df['counter'].fillna(method="ffill",inplace=True)
df['Rows_since_condition'] = df.index-df['counter']
df.drop(['counter'], axis=1,inplace=True)

#Print Results
df

Output:

enter image description here

Sobriety answered 18/10, 2019 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.