Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of zeros after last non-zero value per row

I have the following df:

index jan feb marc april
One 1 7 0 0
two 0 8 7 0
three 0 0 0 1

I'd like to get the number of zeros after the last non-zero value per row. So the output should look like

index num
One 2
two 1
three 0
like image 400
lakadibo Avatar asked Aug 31 '25 17:08

lakadibo


1 Answers

import pandas as pd

# Dataframe
data = {
    'jan': [1, 0, 0],
    'feb': [7, 8, 0],
    'marc': [0, 7, 0],
    'april': [0, 0, 1]
}
df = pd.DataFrame(data, index=['One', 'two', 'three'])

# Calculate the number of zeros after the last non-zero value per index/row
num_zeros = df.ne(0).iloc[:, ::-1].cumsum(axis=1).eq(0).sum(axis=1)

# Result dataframe
result = pd.DataFrame({'num': num_zeros}, index=df.index)

print("DataFrame:")
print(df)
print("\nResult:")
print(result)

Calculating the # of zeroes (df.ne(0).iloc[:, ::-1].cumsum(axis=1).eq(0).sum(axis=1)):

  • ne method to check inequality with zero (df.ne(0))
  • perform cumulative sum along the columns in reverse order (iloc[:, ::-1].cumsum(axis=1)) to get a binary representation of non-zero values after the last non-zero value
  • check where the cumulative product equals zero (eq(0)) and sum along the rows (sum(axis=1)) to get the count

Prints:

DataFrame:
       jan  feb  marc  april
One      1    7     0      0
two      0    8     7      0
three    0    0     0      1

Result:
       num
One      2
two      1
three    0
like image 76
BrJ Avatar answered Sep 02 '25 07:09

BrJ