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?
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?
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()
You can create group for consecutive rows
# create group for consecutive numbers
df['grp'] = (df['B'] != df['B'].shift()).cumsum()
B grp
0 1 1
1 3 2
2 4 3
3 0 4
4 0 4
5 11 5
6 1 6
7 15 7
8 0 8
9 0 8
10 0 8
11 87 9
# check size of groups having 0 value
max_count = df.query("B == 0").groupby('grp').size().max()
print(max_count)
3
Idea is create mask with cumulative sum for counter of consecutive values, filter only 0 values, count them by Series.value_counts and get maximum value:
s = df['B'].ne(0)
a = s.cumsum()[~s].value_counts().max()
print (a)
3
df_out=pd.DataFrame({'max_count':[a]})
Details:
print (s.cumsum())
0 1
1 2
2 3
3 3
4 3
5 4
6 5
7 6
8 6
9 6
10 6
11 7
Name: B, dtype: int32
print (s.cumsum()[~s])
3 3
4 3
8 6
9 6
10 6
Name: B, dtype: int32
print (s.cumsum()[~s].value_counts())
6 3
3 2
Name: B, dtype: int64
Maybe you could adjust it to Python. In Java, you could find most consecutive 0's length using this code:
int B [] = {1,3,4,0,0,11,1,15,0,0,0,87}
int max_zeroes = 0;
int zeroes = 0;
for(int i = 0; i < B.length; i++) {
if( B[i] == 0) {
zeroes += 1;
if(zeroes > max_zeroes) {
max_zeroes = zeroes;
}
} else {
zeroes = 0;
}
}
And if you are inclined towards finding the start and end indexes of most consecutive 0s in an array, you could use this logic:
int max_zeroes = 0;
int zeroes = 0;
int endIndex = -1;
for (int i = 0; i < B.length; i++) {
if (B[i] == 0) {
zeroes += 1;
if (zeroes > max_zeroes) {
max_zeroes = zeroes;
endIndex = i;
}
} else {
zeroes = 0;
}
}
int startIndex = endIndex;
for (int i = endIndex - 1; i > -1; i--) {
if(B[i] == 0) {
start = i;
} else {
i = -1; //used to get out of this for loop.
}
}
System.out.println("Max zeroes is: " + max_zeroes + " at start index " + start + " and end index: " + endIndex);
Maybe you could adjust it to Python.