pythonbeginner
Pandas Cross-Tabulation (crosstab)
Compute frequency and proportion cross-tabulations between two categorical columns.
pythonPress ⌘/Ctrl + Shift + C to copy
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.
pythonbeginner
Value Counts with Normalisation
Compute frequency distributions and percentage breakdowns of categorical columns.
Best for: data profiling
#pandas#value-counts
pythonintermediate
Pareto / Cumulative Share Analysis
Calculate cumulative share (Pareto 80/20) of values for product or customer ranking analysis.
Best for: product analytics
#pandas#pareto
pythonbeginner
Pandas Rank with Tie-Breaking Methods
Apply different ranking strategies (min, dense, average) and handle ties in pandas.
Best for: leaderboards
#pandas#ranking
pythonbeginner
Pandas nlargest / nsmallest
Efficiently retrieve the N largest or smallest rows without sorting the full DataFrame.
Best for: top-N queries
#pandas#top-n