0

I have the following dataframe:

   1   2   3   4   5   6   7  8  9
0  0   0   1   0   0   0   0  0  1
1  0   0   0   0   1   1   0  1  0
2  1   1   0   1   1   0   0  1  1
...

I want to get for each row the longest sequence of value 0 in the row. so, the expected results for this dataframe will be an array that looks like this:

[5,4,2,...]

as on the first row, maximum sequenc eof value 0 is 5, ect.

I have seen this post and tried for the beginning to get this for the first row (though I would like to do this at once for the whole dataframe) but I got errors:

s=df_day.iloc[0]
(~s).cumsum()[s].value_counts().max()

TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

when I inserted manually the values like this:

s=pd.Series([0,0,1,0,0,0,0,0,1])
(~s).cumsum()[s].value_counts().max()

>>>7

I got 7 which is number of total 0 in the row but not the max sequence. However, I don't understand why it raises the error at first, and , more important, I would like to run it on the end on the while dataframe and per row.

My end goal: get the maximum uninterrupted occurance of value 0 in a row.

Reut
  • 1,555
  • 4
  • 23
  • 55

3 Answers3

3

Vectorized solution for counts consecutive 0 per rows, so for maximal use max of DataFrame c:

#more explain https://stackoverflow.com/a/52718619/2901002
m = df.eq(0)
b = m.cumsum(axis=1)
c = b.sub(b.mask(m).ffill(axis=1).fillna(0)).astype(int)
print (c)
   1  2  3  4  5  6  7  8  9
0  1  2  0  1  2  3  4  5  0
1  1  2  3  4  0  0  1  0  1
2  0  0  1  0  0  1  2  0  0

df['max_consecutive_0'] = c.max(axis=1)
print (df)
   1  2  3  4  5  6  7  8  9  max_consecutive_0
0  0  0  1  0  0  0  0  0  1                  5
1  0  0  0  0  1  1  0  1  0                  4
2  1  1  0  1  1  0  0  1  1                  2
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

Use:

df = df.T.apply(lambda x: (x != x.shift()).astype(int).cumsum().where(x.eq(0)).dropna().value_counts().max())

OUTPUT

0    5
1    4
2    2
Muhammad Hassan
  • 4,079
  • 1
  • 13
  • 27
1

The following code should do the job.

the function longest_streak will count the number of consecutive zeros and return the max, and you can use apply on your df.

from itertools import groupby
    def longest_streak(l):
      lst = []
      for n,c in groupby(l):
        num,count = n,sum(1 for i in c)
        if num==0:
          lst.append((num,count))

  maxx = max([y for x,y in lst])
  return(maxx)

df.apply(lambda x: longest_streak(x),axis=1)
David
  • 871
  • 1
  • 5
  • 13
  • I have tried this on a dataframe and got an error for specific row : ~~~Input In [43], in longest_streak(l) 6 if num==0: 7 lst.append((num,count)) ----> 8 maxx = max([y for x,y in lst]) 9 return(maxx) ValueError: ('max() arg is an empty sequence', 'occurred at index 684')~~~ . Do you know why that happens? I can tell that this row has only values 1. – Reut May 17 '22 at 08:42
  • can u share an example? – David May 18 '22 at 08:26