简体   繁体   中英

how to find Max_winning_streak (max number of consecutive +ve values) in pandas dataframe

I have datafram like This:

dataframe_name-> p_and_l

date             pnl
1/2/17 15:14    -907.5
1/3/17 15:14    1685.75
1/4/17 15:14    817
1/5/17 15:14    -182.5
1/6/17 15:14    415.25
1/9/17 15:14    -339.75
1/10/17 15:14   -413
1/11/17 15:14   1137.5
1/12/17 15:14   127.25
1/13/17 15:14   617.5
1/16/17 15:14   -875
1/17/17 15:14   158
1/18/17 15:14   -498.75
1/19/17 15:14   224.5

I tried to find Max_winning_streak (max number of consecutive +ve values) like this... Max_winning_streak = pd.Series(p_and_l['pnl']) Max_winning_streak.apply(consecutiveCount)

But not getting the desired solution.

Can anyone please help me to resolve this... Thanks in advance

Compare values for groups by less ot equal 0 with cumulative sum and then count values, Series.value_counts sorting by default, so first value is maximal count:

m = df['pnl'].le(0)

max1 = m.cumsum()[~m].value_counts().iat[0]
print (max1)
3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM