In this article we will discuss how to get the frequency count of unique values in a dataframe column or in dataframe index. Also either count values by grouping them in to categories / range or get percentages instead of exact counts.

Suppose we have a Dataframe i.e.

Contents of the dataframe empDfObj are,

Frequency count of elements in the column ‘Age’ is,

Now to get the frequency count of elements in index or column like above, we are going to use a function provided by Series i.e.

pandas.Series.value_counts

Arguments :

  • normalize : boolean, default False
    • If True it will return relative frequencies
  • sort : boolean, default True
    • Sort by frequency Count.
  • ascending : boolean, default False
    • Sort by frequency Count in ascending order if True

It returns a Series object containing the frequency count of unique elements in the series.
We can select the dataframe index or any column as a Series. Then using Series.value_counts() we can find the frequency count of elements inside it. Let’s see some examples,

Contents of the dataframe empDfObj are,

Get Frequency count of values in a Dataframe Column

We can select a column in dataframe as series object using [] operator. On calling value_counts() on this Series object, it returns an another Series object that contains the frequency counts of unique value in the calling series i.e. our selected column.
Let’s get the frequency count of unique values in column ‘Age’ of the dataframe empDfObj,

Output

Get Frequency Count of an element in Dataframe index

On similar lines, we can select a Dataframe index using Datframe.index as Series object. Then by calling value_counts() on this Series object, we can get the frequency count of values in the dataframe index i.e.
Let’s fetch the frequency count of unique value in index of dataframe empDfObj,

Output

Get Frequency count of values in a Dataframe Column including NaN

By default value_counts() skips the NaN in series while counting for frequency of unique elements. If we pass the dropna argument as False then it will include NaN too.
For example,

Output

Get Frequency of values as percentage in a Dataframe Column

Instead of getting the exact frequency count of elements in a dataframe column, we can normalize it too and get the relative value on the scale of 0 to 1 by passing argument normalize argument as True. Let’s get the frequency of values in the column ‘City‘ as percentage i.e.

Output

Count of column values in grouped categories

Instead of getting exact frequency count or percentage we can group the values in a column and get the count of values in those groups.
Let’s group the values inside column Experience and get the count of employees in different experience level (range) i.e.

Output

So, basically distributed the values of column ‘Experience’ in 3 different categories / range and returns the count of elements in that range.

Complete example is as follows,

Output:

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,