user2478961
user2478961

Reputation: 25

count non zero column values in pandas

have

Want

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

help please..

Upvotes: 0

Views: 141

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 29982

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

Upvotes: 1

Related Questions