How to create frequency tables in Python?

By Shivang Yadav Last updated : November 21, 2023

Frequency Table in Python

The frequency table in Python is a table that displays the frequency table for a data set i.e. the count of occurrence of data in the dataset. It displays the count of all unique values.

Using Python, we can create a frequency table for all values of the dataset.

Creating a One-way Frequency table

This type of frequency table displays the frequency of only one type of data from the dataset. For this, the crosstab() method present in the pandas library is used.

Syntax

pandas.crosstab(index, columns, value=None, rownames=None, column=None, aggfunc=None, margins=False, margin_name= ‘All’, dropna=True, normalize=False)

Parameters

  • index: values to group by in rows, (series/list/array/series)
  • columns: values to group by in columns, (series/list/array/series)
  • values (optional): values to aggregate based on factors defined by aggfunc.
  • rowname (default value - none): sequence of names for rows, count must be equal to the number of rows passed.
  • column (default value - none): The sequence of names for column count must be equal to the number of columns passed.
  • aggfunction (optional): aggregate function to be specified with values parameter.
  • margin (value - boolean, default-False): States whether to add a row/column displaying subtotal to the result.
  • margins_name (value - str, default - ‘All’): States which row or column will contain the totals when the margin is True.
  • dropNa (value - boolean, default - True): States whether or to not include columns with NaN values.
  • normalize: States weather to normalize values by dividing with the sum of values.

Example: Python program to create frequency table

# Program to create Frequency Table in Python
import pandas as pd

dataset = pd.DataFrame(
    {
        "Category": ["A", "B", "A", "C", "B", "A", "C", "A", "B", "B"],
        "score": [8, 6, 9, 5, 7, 8, 5, 8, 7, 7],
    }
)

print("The values of data set is \n", dataset)

freqTable = pd.crosstab(index=dataset["Category"], columns="count")

print("The frequency table is \n", freqTable)

The output of the above program is:

The values of data set is 
   Category  score
0        A      8
1        B      6
2        A      9
3        C      5
4        B      7
5        A      8
6        C      5
7        A      8
8        B      7
9        B      7
The frequency table is 
 col_0     count
Category       
A             4
B             4
C             2

Creating Two-Way Frequency Tables

Python also allows two-way frequency tables based on two parameters.

Example: Python program to create two-way frequency table

import pandas as pd

dataset = pd.DataFrame(
    {
        "Category": ["A", "B", "A", "C", "B", "A", "C", "A", "B", "B"],
        "score": [8, 6, 9, 5, 7, 8, 5, 8, 7, 7],
    }
)

print("The values of data set is \n", dataset)

freqTable = pd.crosstab(index=dataset["Category"], columns=dataset["score"])

print("The frequency table is \n", freqTable)

The output of the above program is:

The values of data set is 
   Category  score
0        A      8
1        B      6
2        A      9
3        C      5
4        B      7
5        A      8
6        C      5
7        A      8
8        B      7
9        B      7
The frequency table is 
 score     5  6  7  8  9
Category               
A         0  0  0  3  1
B         0  1  3  0  0
C         2  0  0  0  0

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.