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

## What is a contingency table?

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.

**📚 Discover Online Data Science Courses & Programs (Enroll for Free)**

**Introductory ⭐**

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Shirt Id | Color | Size |

1 | Red | M |

2 | Blue | S |

3 | Orange | L |

4 | Blue | L |

5 | Red | S |

6 | Red | M |

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

S | M | L | |

Red | 1 | 2 | 0 |

Blue | 1 | 0 | 1 |

Orange | 0 | 0 | 1 |

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

**Upskill your career right now →**

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

## Contingency Table in Python

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:

**Upskill your career right now →**

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.**