Nickel
Nickel

Reputation: 590

How to check maximum of consecutive values greater than 5

I want to check in dataframe for how many rows consecutive values greater than 5.

df=pd.DataFrame({'A':[3,4,7,8,11,6,15,3,15,16,87]})

out:
df=pd.DataFrame({'count_greater_5_max':[5]}) 

Upvotes: 1

Views: 90

Answers (1)

jezrael
jezrael

Reputation: 862661

Use:

#compare greater like 5
a = df.A.gt(5)
#running sum
b = a.cumsum()
#counter only for consecutive values
out = b-b.mask(a).ffill()
#maximum value of counter
print (int(out.max()))
5

df=pd.DataFrame({'count_greater_5_max':[int(out.max())]}) 

Upvotes: 1

Related Questions