What is pandas crosstab() and how does it differ from pivot_table()?

Quick Overview

Explain what the pandas crosstab() function does and how it differs from pivot_table(). Tests your knowledge of contingency tables, default aggregations, normalization, margins and how each function handles empty combinations.

What is pandas crosstab() and how does it differ from pivot_table()?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

In pandas, what is `crosstab()` and how does it differ from `pivot_table()`? ```hint Compare the defaults Look at what inputs each function accepts and what each one computes when you pass only row and column keys. ``` ### What a Strong Answer Covers - What `crosstab()` computes and what inputs it accepts - What `pivot_table()` computes, its default aggregation, and how it relates to `crosstab()` - Differences in defaults: the aggregation, normalization, and how empty combinations are filled - When to choose each one, with an example call ### Follow-up Questions - How would you get row percentages from `crosstab()`, and how would you build the same count table with `groupby()`? - What is the difference between `pivot()` and `pivot_table()`, and when does `pivot()` raise an error? - How do you compute different aggregations of different columns in a single `pivot_table()` call? - How would you use a `crosstab()` result to test whether two categorical variables are independent?

Overview: Explain what the pandas crosstab() function does and how it differs from pivot_table(). Tests your knowledge of contingency tables, default aggregations, normalization, margins and how each function handles empty combinations.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

In pandas, what is crosstab() and how does it differ from pivot_table()?

What a Strong Answer Covers Guidance

  • What crosstab() computes and what inputs it accepts
  • What pivot_table() computes, its default aggregation, and how it relates to crosstab()
  • Differences in defaults: the aggregation, normalization, and how empty combinations are filled
  • When to choose each one, with an example call

Follow-up Questions Guidance

  • How would you get row percentages from crosstab() , and how would you build the same count table with groupby() ?
  • What is the difference between pivot() and pivot_table() , and when does pivot() raise an error?
  • How do you compute different aggregations of different columns in a single pivot_table() call?
  • How would you use a crosstab() result to test whether two categorical variables are independent?