2

I Have one dataframe with column Flag1 in this, I want to check if in column flag value 1 occurs continuously for maximum times

Here is the dataframe and output format

df = pd.DataFrame({'flag':[1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1]}) 
df_out = pd.DataFrame({'max_count':[3]})
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Nickel
  • 580
  • 4
  • 19

3 Answers3

5

Solution with pandas

m = df['flag'].eq(1)
max(m[m].groupby((~m).cumsum()).sum())

Solution with itertools.groupby

from itertools import groupby

max(sum(g) for k, g in groupby(df['flag']) if k == 1)

Result

3
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53
3

We can emulate itertools.groupby style grouping using df.shift +df.ne

m = df['flag'].eq(1)
g = df['flag'].ne(df['flag'].shift()).cumsum()[m]

df[m].groupby(g).count().max()

# flag    3
# dtype: int64
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • I like this solution. Upvoted. Can we simplify the definition of `g` without selection by `[m]` if we are going to use `df[m]` in the last line of code ? – SeaBean Jul 24 '21 at 12:23
  • 1
    @SeaBean Thanks. Grouping array length should be equal to the length of `df`. – Ch3steR Jul 24 '21 at 12:51
2

I provided an answer to a similar problem here:

You can do:

s = df['flag']

(s.groupby(((s-s.shift().fillna(0)).eq(1).cumsum()*s))
  .transform(len)*s
).max()

output: 3

mozway
  • 194,879
  • 13
  • 39
  • 75