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()