All lessons

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.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 4 · about 15 minutes · uses Lesson 4.1. You’ll be able to build frequency tables and two-way tables with percentages.

value_counts counts how often each value appears; crosstab counts every combination of two columns in a grid.

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

Pythoncustomers.csv

Add dropna=False to count missing values too; by default they’re left out.

2. crosstab

Pythoncustomers.csv

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:

The rows. segment by store for 6 customers: S01 Premium, S01 Regular, S01 Regular, WEB Premium, WEB Premium, WEB Regular.
Counts. pd.crosstab(df.store, df.segment)
storePremiumRegular
S0112
WEB21
Shares per row. normalize='index'
storePremiumRegular
S010.330.67
WEB0.670.33
Read it. WEB customers are twice as often Premium.

3. Your turn

Your turncustomers.csv
Your turnorders.csv
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

  1. Count order lines by discount level, including how many have none.
  2. What question does normalize='columns' answer in a store × segment table?
  3. How would you test whether segment depends on store?
Answers
  1. orders['discount'].value_counts().sort_index().
  2. “Of each segment’s customers, what share belongs to each store?”
  3. 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

1How do you get shares instead of counts from value_counts?
2What does pd.crosstab(df.store_id, df.segment) show?
3In crosstab, normalize='index' makes:

Sources

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.