Home > Software design >  count non zero column values in pandas
count non zero column values in pandas

Time:05-18

have

Want

In a pandas dataframe, I need a Count column that counts non zero occurrences

help please..

CodePudding user response:

Try

df['Count'] = df.filter(regex='col\d').ne(0).sum(axis=1)
print(df)

  group_col  col1  col2  col3  Count
0    group1    23     0    32      2
1    group2     1     1    38      3
2    group3     0     9    97      2
  • Related