How to get the number per row of columns that are not 0

1 min

I had to find a way to get the count of columns per row that weren't 0.

This is how I've done it using the DataFrame.astype().

import pandas as pd

# We generate a dataframe with dummies data
df = pd.get_dummies(["col1","col2","col3"])

# We return the number of columns that are not 0 per row.
df.astype(bool).sum(axis=1)
How to get the number per row of columns that are not 0

As you can't use the DataFrame.count() method to do this since it takes 0 as a value you have to use the DataFrame.astype() method then sum each row's values to have the final count.

As you can see we first transform (cast) our DataFrame into boolean type.

Then we sum up using the DataFrame.sum() method by specifying the axis=1 so that we have a sum per row.

Here you are! Now you can get the count of columns that are not 0.

More on DataFrames

If you want to know more about DataFrame and Pandas. Check out the other articles I wrote on the topic, just here :

Pandas - The Python You Need
We gathered the only Python essentials that you will probably ever need.