Home > other >  How to count non-zero values in a dataframe inside a range of a column
How to count non-zero values in a dataframe inside a range of a column

Time:07-08

I have a dataframe, and I want to count the non-zero values of a column, the only problem is that I want to count those values in a moving window. For example, I have this:

df.loc[i:j,'data']

Now I want to count all the non-zero values on the column 'data' within this [i:j] range.

CodePudding user response:

You can use

sum(df.loc[i:j,'data'].ne(0))
#or
df.loc[i:j,'data'].ne(0).sum()
  • Related