Module 4: Grouping and aggregating · Lesson 4.4 · about 15 min
value_counts and crosstab
Count how often each value appears with value_counts (as counts or shares), and cross two categories with crosstab to get a frequency table with totals and row or column percentages.
Module 4 · about 15 minutes · uses Lesson 4.1. You’ll be able to build frequency tables and two-way tables with percentages.
In everyday terms: a tally sheet. value_counts is one column of tally marks per answer; crosstab is a grid with one tally box per pair of answers, like a seating chart of who sat where.
1. value_counts
Add dropna=False to count missing values too; by default they’re left out.
2. crosstab
margins=True adds totals. normalize='index' gives row percentages: “of this store’s customers, what share is each segment?” Choose the direction that answers your question; it’s the commonest mistake in reading these tables.
With a value: pd.crosstab(a, b, values=df['revenue'], aggfunc='sum') sums a number instead of counting rows, like a pivot table (Lesson 6.1).
- value_counts()
- Counts per value, biggest first.
- normalize=True
- Shares instead of counts.
- pd.crosstab(a, b)
- Counts of each a-b combination.
- normalize='index'
- Shares within each row.
- Physical meaning
- A crosstab is the table a chi-square test reads (Statistics Lesson 6.5): if the row shares look alike, the two columns are unrelated; if they differ, one tells you something about the other.
“Compare the counts across rows.” Rows with more customers have bigger counts everywhere. Compare shares within each row (normalize=‘index’) to see whether the mix really differs.
Watch it happen, one step at a time:
pd.crosstab(df.store, df.segment)| store | Premium | Regular |
|---|---|---|
| S01 | 1 | 2 |
| WEB | 2 | 1 |
normalize='index'| store | Premium | Regular |
|---|---|---|
| S01 | 0.33 | 0.67 |
| WEB | 0.67 | 0.33 |
3. Your turn
How it works
Principle: a crosstab is a groupby on two keys with size(), unstacked so one key runs across the columns. It’s also the input to a chi-square test of independence (Statistics Lesson 6.5): are segment and store related, or would the mix look like this by chance?
Which percentage: row % compares the make-up of rows; column % compares the make-up of columns; total % shows each cell’s share of everything.
4. Practice
- Count order lines by discount level, including how many have none.
- What question does
normalize='columns'answer in a store × segment table? - How would you test whether segment depends on store?
Answers
orders['discount'].value_counts().sort_index().- “Of each segment’s customers, what share belongs to each store?”
- A chi-square test of independence on the count crosstab, such as with the cross-tab and chi-square tool.
Try it on your own data: cross-tab and chi-square builds the table and runs the test.
New words: value_counts, normalize, crosstab, margins, row percentage.
- value_counts gives counts or shares of one column.
- crosstab counts combinations of two columns.
- normalize=‘index’ compares mixes fairly across rows.
- A crosstab is the input to a chi-square test.
Check yourself
Sources
- pandas documentation, User Guide: Reshaping and pivot tables (crosstab) https://pandas.pydata.org/docs/user_guide/reshaping.html#cross-tabulations
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.