0Pricing
Data Science Academy · Lesson

pivot_table for Cross-Tabs

Summarizing across two dimensions.

pivot_table for Cross-Tabs is a free Data Science Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From Long Rows to a Grid

When you want a category-by-category summary, pivot_table turns tidy long rows into a clean two-dimensional grid. 🔄

The Three Key Arguments

Every pivot table needs three roles: which column becomes index, which becomes columns, and which values fill the cells.

df.pivot_table(index='region',
               columns='month',
               values='sales')

It Aggregates by Default

If several rows land in the same cell, pivot_table averages them. The default aggfunc is mean, not a plain copy.

Choose Your Aggregation

Swap the math with aggfunc. Use sum for totals, count for tallies, or max to spot the peak value per cell.

df.pivot_table(index='region',
               values='sales',
               aggfunc='sum')

Many Aggregations at Once

Pass a list to aggfunc and get several summaries together. One call can return both the mean and the count side by side.

df.pivot_table(values='sales',
               aggfunc=['mean', 'count'])

Filling Empty Cells

Missing combinations show up as NaN. Set fill_value to replace those gaps with zero or any sensible default.

df.pivot_table(index='region',
               columns='month',
               fill_value=0)

Add Row and Column Totals

Set margins to True and pivot_table adds an All row and column with grand totals around the edges.

df.pivot_table(index='region',
               margins=True)

Group by Several Keys

Pass a list to index or columns to nest categories. The result gains a MultiIndex that stacks the groups neatly.

df.pivot_table(index=['region', 'store'],
               values='sales')

pivot vs pivot_table

Plain pivot needs unique index-column pairs and cannot aggregate. Reach for pivot_table whenever duplicates might appear.

Reading a Cross-Tab

In the result, scan a cell by its row label and column label to read that exact category combination at a glance.

Back to a Flat Table

A pivot result is still a DataFrame. Call reset_index to flatten its labels back into ordinary columns for export.

summary.reset_index()

Quick Check

Let us check your pivot_table reflexes.

Recap: Cross-Tabs Made Easy

You built grids with index, columns, values, and aggfunc. Next you will reverse course and melt wide data back to long. 🎯

Frequently asked questions

Is the “pivot_table for Cross-Tabs” lesson free?

Yes — the full text of “pivot_table for Cross-Tabs” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.

What will I learn in “pivot_table for Cross-Tabs”?

Summarizing across two dimensions. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Data Science Academy?

No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “pivot_table for Cross-Tabs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Data Science Academy lesson?

Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Wide vs Long, and Tidy Data
  2. pivot_table for Cross-Tabs
  3. melt to Go Long
  4. stack, unstack, and MultiIndex
← Back to Data Science Academy