0Pricing
Pandas & NumPy Academy · Lesson

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  2

All lessons in this course

  1. pivot_table: Cross-Tabulation
  2. melt: Wide to Long Format
  3. stack and unstack with MultiIndex
  4. crosstab for Frequency Tables
← Back to Pandas & NumPy Academy