How to create a contingency table in Python?

By Shivang Yadav Last updated : November 21, 2023

Contingency Table

A contingency table is a table that represents the relationship between two categorical tables. It shows the frequency distribution of variables along with their relationships.

Creating Contingency Table

Python allows programmers to create a contingency table for tables present in a dataset. To create a contingency table, use the pandas.crosstab() method which computes simple cross-tabulation of two (or more) factors.

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, the 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 a contingency table

# Program to create a contingency table
import pandas as pd

# Create data
df = pd.DataFrame(
    {
        "Sport": [
            "Cricket",
            "Football",
            "BasketBall",
            "Cricket",
            "Cricket",
            "BasketBall",
            "Cricket",
            "Football",
        ],
        "Rating": ["A", "B", "A", "B", "B", "A", "C", "B"],
    }
)

print("Table data \n", df)
conTable = pd.crosstab(index=df["Sport"], columns=df["Rating"])

print("Contingency table \n", conTable)

Output

The output of the above program is:

Table data 
         Sport Rating
0     Cricket      A
1    Football      B
2  BasketBall      A
3     Cricket      B
4     Cricket      B
5  BasketBall      A
6     Cricket      C
7    Football      B
Contingency table 
 Rating      A  B  C
Sport              
BasketBall  2  0  0
Cricket     1  2  1
Football    0  2  0

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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