Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find maximum count of consecutive zeros in column pandas?

I have dataframe and want to check maximum count of consecutive zero values in Column B.

Example input and output:

df = pd.DataFrame({'B':[1,3,4,0,0,11,1,15,0,0,0,87]})

df_out = pd.DataFrame({'max_count':[3]})

How could this be done?

like image 322
Edward Avatar asked Sep 04 '20 08:09

Edward


Video Answer


1 Answers

One NumPy way -

a = df['B'].values
m1 = np.r_[False, a==0, False]
idx = np.flatnonzero(m1[:-1] != m1[1:])
out = (idx[1::2]-idx[::2]).max()

Step-by-step run -

# Input data as array
In [83]: a
Out[83]: array([ 1,  3,  4,  0,  0, 11,  1, 15,  0,  0,  0, 87])

# Mask of starts and ends for each island of 0s
In [193]: m1
Out[193]: 
array([False, False, False, False,  True,  True, False, False, False,
        True,  True,  True, False, False])

# Indices of those starts and ends
In [85]: idx
Out[85]: array([ 3,  5,  8, 11])

# Finally the differencing between starts and ends and max for final o/p
In [86]: out
Out[86]: 3

That could be converted to a one-liner :

np.diff(np.flatnonzero(np.diff(np.r_[0,a==0,0])).reshape(-1,2),axis=1).max()
like image 108
Divakar Avatar answered Nov 10 '22 15:11

Divakar