crosstab()
noun
say it: “KRAWS-tab” /ˈkrɔsˌtæb/
A pandas function that counts how many times each pair of values from two columns appears, laid out as a grid.
What does crosstab() do in pandas?
crosstab(), written pd.crosstab, counts how often each combination of two columns' values happens, such as each
neighborhood with each pizza size. normalize="index" turns each row's counts into shares of that row.
import pandas as pd
orders = pd.DataFrame({"neighborhood": ["Elm Park", "Elm Park", "Riverside"], "size": ["small", "large", "large"]})
print(pd.crosstab(orders["neighborhood"], orders["size"]))
# size large small
# neighborhood
# Elm Park 1 1
# Riverside 1 0
In a sentence
- “With normalize, crosstab() turned each row into shares that add up to 1.”
- “crosstab() showed how many orders of each size came from each neighborhood.”
Level: Journeyman · More Python terms at this level
Updated