pythonbeginner

Pandas Cross-Tabulation (crosstab)

Compute frequency and proportion cross-tabulations between two categorical columns.

python
import pandas as pd
import numpy as np

df = pd.DataFrame({'dept': np.random.choice(['eng','hr','finance'], 200),'level': np.random.choice(['junior','mid','senior'], 200)})

# Frequency count
ct = pd.crosstab(df['dept'], df['level'])
print(ct)

# Normalised (proportions per row)
ct_norm = pd.crosstab(df['dept'], df['level'], normalize='index')
print(ct_norm.round(2))

# With margins
print(pd.crosstab(df['dept'], df['level'], margins=True))

Use Cases

  • categorical analysis
  • headcount reporting
  • segment breakdown

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.