Cross Tabulation
pd.crosstab() computes frequency tables to show how often value combinations occur.
Basic Crosstab
Count combinations of values:
Loading Pandas Playground...
Adding Margins
Include row and column totals:
Loading Pandas Playground...
Normalizing Values
Show percentages instead of counts:
Loading Pandas Playground...
Aggregating Values
Apply functions to a third column:
Loading Pandas Playground...
Multiple Values
Crosstab with multiple row or column variables:
Loading Pandas Playground...
Crosstab vs Pivot Table
Compare the two approaches:
Loading Pandas Playground...
Exercise: Basic Crosstab
Loading Exercise...
Exercise: Percentage Crosstab
Loading Exercise...
Key Points
pd.crosstab()creates frequency tables- Counts combinations by default
margins=Trueadds totalsnormalize=for percentages ('all', 'index', 'columns')- Use
values=andaggfunc=to aggregate other data - Simpler than pivot_table for counting

