Introduction to Frequency Tables in Python

Frequency Tables FeaImg

A table that depicts the frequency of occurrences of several categories is called a frequency table. This particular kind of table is especially helpful for gaining an idea of the distribution of the values contained in a dataset. This tutorial will walk you through the process of creating frequency tables in Python. We will be covering implement of the same in a number of different ways which are covered in the next few sections.

Also Read: 6 Ways to Count Pandas Dataframe Rows

Method 1 – With the help of value_counts() function

The very first method is to make use of the value_counts() function which will return a series containing the count of unique values in all the list of values. The result will be in descending order which implies that the first element is the most frequently-occurring element.

import pandas as pd

data = pd.Series([1, 2, 5, 2, 3, 3, 3, 3, 4, 4, 5])

print("The Dataset is : ")

print("\nFrequency Table for the data : ")
The Dataset is : 
0     1
1     2
2     5
3     2
4     3
5     3
6     3
7     3
8     4
9     4
10    5
dtype: int64

Frequency Table for the data : 
3    4
2    2
5    2
4    2
1    1

Method 2 – With the help of crosstab() function

Another function that we can use to display frequencies of a pandas DataFrame is the crosstab() function as shown in the code below. We will create a dataframe and then create the frequency table for two columns of the data frame.

df = pd.DataFrame({'Student_Grade': ['A','B','A','B','B', 'B', 'B', 'C', 'C', 'D'],
                   'Student_Age': [18, 25, 28, 19, 30, 20, 15, 18, 29, 17],
                   'Student_Gender': ['M','F', 'M', 'F', 'F', 'M', 'M', 'F', 'F', 'F']})

print("The Dataset is : ")

print("\nFrequency Table for the Grade in the dataset : ")
pd.crosstab(index=df['Student_Grade'], columns='count')
Crosstab Output Screenshot
Crosstab Output Screenshot
print("\nFrequency Table for the Gender in the dataset : ")
pd.crosstab(index=df['Student_Gender'], columns='count')
Crosstab Output Screenshot 2
Crosstab Output Screenshot 2

Advance Frequency Tables ( 2 – way Tables )

We can also create a two-way frequency table to display the frequencies for two different columns in the dataset we used in the last section. The following code displays a two-way frequency table for the two columns Age and Grade.

pd.crosstab(index=df['Student_Grade'], columns=df['Student_Age'])
Two Way Freq Table Output 1
Two Way Freq Table Output 1

We will also be developing a two-way frequency table between the two columns Gender and Grade. Look at the code below.

pd.crosstab(index=df['Student_Grade'], columns=df['Student_Gender'])
Two Way Freq Table Output 2
Two Way Freq Table Output 2

Thank you for reading! I hope you understood the tutorial 😃

I would recommend you to give the following tutorials a read as well:

  1. Calculating Precision in Python — Classification Error Metric
  2. Chi-square test in Python — All you need to know!!
  3. Universal NumPy Trigonometric functions to know