In this lesson, you will learn how to use cross-tabulations to reveal the hidden relationships and overlaps between multiple categorical variables in your dataset.
Understanding Cross-Tabulation
While you have mastered grouping data and pivot tables, a cross-tabulation (or "crosstab") is a specialized form of frequency analysis used specifically to observe the intersection of categorical variables. Instead of calculating a mean or sum, a crosstab counts the number of occurrences of each combination of categories, providing a clear bird's-eye view of how two or more variables interact.
Think of it as a contingency table. If you are analyzing a retail dataset, you might want to know how many customers from each "Region" purchased a specific "Product Category." A crosstab will map regions on the rows and product categories on the columns, populating the intersection cells with the frequency counts.
Implementation with Pandas
To compute a crosstab, we use the pd.crosstab() function. It is designed to take two or more arrays (or Series) and return a frequency table formatted as a DataFrame.
import pandas as pd
# Sample data: customer segments
data = {'Region': ['North', 'North', 'South', 'North', 'South'],
'Category': ['Electronics', 'Home', 'Electronics', 'Electronics', 'Home']}
df = pd.DataFrame(data)
# Compute the frequency of Category based on Region
freq_table = pd.crosstab(index=df['Region'], columns=df['Category'])
print(freq_table)
In the resulting table, the values at the intersection represent the count of how many times a specific "Region" saw a specific "Category."
Advanced Analysis
You can extend this functionality beyond simple counts. By adding the margins=True parameter, you can include row and column subtotals, which are vital for understanding the weight of each category relative to the whole. If you prefer proportions instead of raw counts, you can normalize the data using normalize='index' or normalize='columns', allowing you to see the percentage distribution across your segments rather than just the frequency.
Key Takeaway: Cross-tabulation allows you to identify dependencies and overlaps between categorical variables by counting occurrences at every intersection of your data segments.