Knowledge base / Python

pandas crosstab: counts, shares and the right denominator

Where do returns come from, and what share of orders are returned? I compare the denominators in crosstab and explain how to read each result.

Kajo RudzińskiPublished Updated 5 min read

Two streams of blocks feeding four table compartments, in yellow and navy pixel art.

A cross-tabulation can answer several questions that sound similar. How many returns came from the store? What share of store orders were returned? What share of all returns came from the store? Choosing the wrong denominator can produce a correctly calculated number with an incorrect explanation.

I start with counts and add proportions later. This entry uses eight synthetic observations and the pandas 3.1.1 documentation available in November 2026. The results are derived from that small table. There is no need for random data or a large example that hides the basic arithmetic.

What crosstab counts

pandas.crosstab counts occurrences of factor combinations by default. The first argument defines row groups and the second defines column groups. For a different aggregation, supply both values and aggfunc.

import pandas as pd

orders = pd.DataFrame({
    "channel": ["Web", "Web", "Web", "Store", "Store", "Store", "Store", "Store"],
    "status": ["Sent", "Sent", "Returned", "Sent", "Sent", "Sent", "Returned", "Returned"],
    "amount": [10, 20, 30, 40, 50, 60, 70, 80],
})

counts = pd.crosstab(orders["channel"], orders["status"])

In this exercise, each row represents one order and status is its chosen classification for the analysis. This is not a history of status changes. If an order appeared first as sent and later as returned, row count would differ from order count. Resolve that distinction before building the table.

channel Returned Sent
Store 2 3
Web 1 2

Store has five observations, Web has three and the total is eight. The Returned column contains three and Sent contains five. A larger return count in Store is insufficient to assess return frequency relative to order volume. The channels have different sizes.

Use margins to keep track of counts

with_totals = pd.crosstab(
    orders["channel"],
    orders["status"],
    margins=True,
)
channel Returned Sent All
Store 2 3 5
Web 1 2 3
All 3 5 8

With complete categories and one row per order, the value 8 should match the input. I would also inspect one cell, because an overall total cannot detect a swapped channel. Use margins_name if “All” is unsuitable for the report’s language.

When combining margins and normalization, interpretation depends on the denominator. Explain the simple table before adding the margin variant. More numbers can make a report harder to read, especially when neighbouring figures have different bases.

Shares of the whole dataset

overall = pd.crosstab(
    orders["channel"],
    orders["status"],
    normalize="all",
)

Divide each cell by 8. Store/Returned is 2 out of 8, or 0.25. Web/Returned is 1 out of 8, or 0.125. All four cells together sum to 1.

channel Returned Sent
Store 0.25 0.375
Web 0.125 0.25

The same basis is selected by normalize=True. These are shares from 0 to 1, so 0.25 means 25%. I would label them “share of all orders”, avoiding the suggestion that they describe returns within each channel. A percentage sign alone does not identify the denominator.

Row composition: returns within a channel

by_channel = pd.crosstab(
    orders["channel"],
    orders["status"],
    normalize="index",
)

Divide Store by 5 and Web by 3. The recurring decimals below are shown to four places for readability; this is a presentation choice rather than an exact transcript of a dtype display.

channel Returned Sent
Store 0.4000 0.6000
Web 0.3333 0.6667

This answers a question about status composition within each channel. Every row sums to 1 before rounding. With such a small sample, I treat the result as a description of these eight observations. It cannot by itself establish a durable channel advantage or explain why returns happen.

Two trays with three and five spaces showing different bases for proportions, in yellow and navy pixel art.

Column composition: where returns come from

by_status = pd.crosstab(
    orders["channel"],
    orders["status"],
    normalize="columns",
)

Divide Returned by 3 and Sent by 5. Store accounts for two thirds of all returns and three fifths of orders classified as Sent. Web accounts for one third and two fifths respectively. Each column sums to 1 before rounding.

This differs from the previous question. “Two thirds of returns came from the store” does not mean that two thirds of store orders were returned. I would include that explanation when the report is intended for people who do not work with proportions every day.

The direction is controlled by normalize. There is no separate normalize_axis parameter in the documented signature. Use named arguments when extending the call so that axis labels, aggregation and denominator choices remain easy to distinguish.

Aggregate amounts instead of observations

amounts = pd.crosstab(
    orders["channel"],
    orders["status"],
    values=orders["amount"],
    aggfunc="sum",
)

Store/Returned combines 70 and 80 to give 150. Store/Sent combines 40, 50 and 60, also giving 150. Both Web cells total 30. The overall amount is 360 in our arbitrary unit. A share based on returned amounts would differ from a share based on return counts.

I would avoid giving all these tables the same ambiguous title, “Returns”. The description must identify observations, amounts or unique people. For more elaborate aggregation layouts, see pandas pivot_table.

Missing values and aligned indexes

Before tabulating, decide how an unknown status should be handled. The missing-data guide describes detection and filling. One deliberate policy is to create a control category:

status_for_report = orders["status"].fillna("Unknown")
counts_with_unknown = pd.crosstab(orders["channel"], status_for_report)

The main dataset has no missing statuses, so this step leaves its classification unchanged. In real data, “Unknown” would mean unknown rather than silently classifying an order as sent or returned. You must also decide whether those observations belong in the denominator of the published measure.

The cross-tabulation guide discusses categories, including showing unobserved levels with dropna=False. I would not describe that as a universal repair for missing data. Series are also aligned by their indexes. In this example, both columns come from the same DataFrame rather than independently filtered Series with different labels.

Exercise: add one new order

Imagine adding one Web order classified as Sent. Web’s return count remains 1, while its denominator rises from 3 to 4. The return share within Web changes from one third to one quarter. Web’s share of all returns remains one third.

If you can explain why one proportion changed while the other stayed the same, you have the central idea of this entry. To group numeric values before making such a table, see pandas cut. Establish the observation and denominator first.

Key takeaways

Does normalize=True return percentages from 0 to 100?

It returns shares of the whole table, so 0.25 means 25%. Multiply a share by 100 if you need the percentage value, and state the denominator in the description.

How do normalize='index' and normalize='columns' differ?

index divides by the corresponding row total, while columns divides by the corresponding column total. The first describes composition within row groups; the second describes composition within column groups.

Does crosstab count unique customers?

By default it counts observations of factor combinations. A customer appearing several times may be counted several times. Define the unit and prepare the input for the question you want to answer.

AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.