How to get the number per row of columns that are not 0
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
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)
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 :
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚