Make Contingency Table in Python

In this tutorial, we will look at how to create a contingency table in Python with the help of some examples.

Contingency table illustration

Contingency table is a tabular representation of counts across two categorical variables. It is used to see the relationship between the two categorical fields. For example, if we have the following data on shirt colors and sizes.

Shirt IdColorSize
1RedM
2BlueS
3OrangeL
4BlueL
5RedS
6RedM

The contingency table between the columns “Color” and “Size” would look like –

SML
Red120
Blue101
Orange001

You can see that the above table summarizes the counts across the two columns. You can interpret the above contingency table as –

  1. For the color Red, we have one shirt with size S, two shirts with size M and no shirts with size L.
  2. For the color Blue, we have one shirt with size S and one shirt with size L.
  3. For the color Orange, we have one shirt with size L.

You can use the pandas crosstab() function to get a contingency table between two categorical columns of a dataframe in Python. The following is the syntax:

# assuming pandas imported as pd
pd.crosstab(df["Col1"], df["Col2"])

We pass the two columns as arguments. It returns the contingency table as a pandas dataframe.

Let’s walk through some examples of using the above syntax to get a contingency table in Python. First, we will create a sample dataframe that we will be using throughout this tutorial.

import pandas as pd

# create a dataframe
df = pd.DataFrame({
    'shirt_id': [1, 2, 3, 4, 5, 6],
    'color': ['Red', 'Blue', 'Orange', 'Blue', 'Red', 'Red'],
    'size': ['M', 'S', 'L', 'L', 'S', 'M']
})
# display the dataframe
print(df)

Output:

   shirt_id   color size
0         1     Red    M
1         2    Blue    S
2         3  Orange    L
3         4    Blue    L
4         5     Red    S
5         6     Red    M

We now have a dataframe containing the colors and sizes of different shirt in a local store’s inventory.

Now, let’s make a contingency table between the categorical columns “color” and “size”.

# contingecy table between "color" and "size"
print(pd.crosstab(df['color'], df['size']))

Output:

size    L  M  S
color          
Blue    1  0  1
Orange  1  0  0
Red     0  2  1

You can see that the above contingency table shows the counts across the two features.

You can also get the row and column totals in the contingency table. For this, pass margins=True to the pandas crosstab() function as an argument. For example, let’s get the same contingency table as above but with row and column totals this time.

# contingecy table with row and column totals
print(pd.crosstab(df['color'], df['size'], margins=True))

Output:

size    L  M  S  All
color               
Blue    1  0  1    2
Orange  1  0  0    1
Red     0  2  1    3
All     2  2  2    6

We now have an additional column for the row totals and an additional row for the column totals.

For more on the pandas crosstab() function, refer to its documentation.

Note that contingency tables are a quite popular method during EDA (exploratory data analysis) as they show relationship between two categorical fields. They are often used to see the distribution of a categorical field with respect to the class label in classification tasks. They are are also used in statistical tests like chi-squared test which test for the association between two categorical variables.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Leave a Reply

Your email address will not be published. Required fields are marked *