Getting frequency counts of a columns in Pandas DataFrame
Given a Pandas dataframe, we need to find the frequency counts of each item in one or more columns of this dataframe. This can be achieved in multiple ways:
Method #1: Using
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
This method is applicable to pandas.Series object. Since each DataFrame object is a collection of Series object, we can apply this method to get the frequency counts of values in one column.
Method #2: Using
This method can be used to count frequencies of objects over single columns. After grouping a DataFrame object on one column, we can apply
count() method on the resulting groupby object to get a DataFrame object containing frequency count.
Method #3: Using
This method can be used to count frequencies of objects over single or multiple columns. After grouping a DataFrame object on one or more columns, we can apply
size() method on the resulting groupby object to get a Series object containing frequency count.