crosstab for Frequency Tables
Compute a frequency or proportion cross-tabulation of two categorical columns with pd.crosstab.
What Is a Crosstab?
A cross-tabulation (crosstab) counts how many times each combination of values from two (or more) categorical variables appears in a dataset. It is a special case of pivot table designed specifically for counting. Pandas provides pd.crosstab() as a concise way to compute these frequency tables without needing to call groupby() or pivot_table() explicitly.
Basic crosstab() Call
The minimal pd.crosstab(index, columns) call takes two array-like inputs (typically DataFrame columns) and returns a frequency table. Rows correspond to unique values in the first argument and columns correspond to unique values in the second. Each cell contains the count of rows where both values co-occur in the original data.
import pandas as pd
df = pd.DataFrame({
'gender': ['M', 'F', 'M', 'F', 'M', 'F', 'M'],
'product': ['A', 'A', 'B', 'B', 'A', 'B', 'B']
})
ct = pd.crosstab(df['gender'], df['product'])
print(ct)
# product A B
# gender
# F 1 2
# M 2 2All lessons in this course
- pivot_table: Cross-Tabulation
- melt: Wide to Long Format
- stack and unstack with MultiIndex
- crosstab for Frequency Tables