How to find the number of data points per class in python?

forget to forget the forgetness… hahaha…

Actually, most of the time we forget the basic features of pandas DataFrame.

I hope you are reading this blog because you forget how to find the number of data points per class, right.

So, there is a very useful method called value_counts() which will help you to get the number of data points for each class.

Let’s see an example below.

source: Created by Author

Story: (you can skip)

Actually, I was trying to solve the problem called DonorsChoose and I was instructed that data is imbalanced so either uses the balanced feature is equal to true in the model or upsample the dataset. So, I was curious to know the distribution of the dataset and I forgot how to do it. I googled sometimes with different keywords but I was not getting the desired results. In the end, I got the result by using the keyword as you read in the title of this blog.

You can also use the count plot to show the above result…

source: Created by author

Thanks a lot.