Cross-tabulation is used to compute a simple frequency table of two or more factors. It helps to understand the relationship between categorical variables by showing counts or other statistics in a matrix format.
Click to reveal answer
beginner
How can you add margins (totals) to a pandas crosstab?
Use the parameter margins=True in pd.crosstab(). This adds row and column totals to the table, helping to see overall counts.
Click to reveal answer
intermediate
What does the normalize parameter do in pd.crosstab()?
The normalize parameter converts counts into proportions. You can normalize by 'index', 'columns', or 'all' to get relative frequencies instead of raw counts.
Click to reveal answer
advanced
How do you include multiple aggregation functions in a pandas crosstab?
Use the aggfunc parameter with a list of functions (e.g., [np.sum, np.mean]) and provide a values argument to specify the data to aggregate.
Click to reveal answer
advanced
Explain how to create a multi-index crosstab with more than two categorical variables.
Pass multiple arrays or columns to the index and/or columns parameters as lists. This creates a multi-level index in the resulting table, showing detailed breakdowns.
Click to reveal answer
Which parameter in pd.crosstab() adds row and column totals?
Anormalize='all'
Bmargins=True
Caggfunc='sum'
Ddropna=False
✗ Incorrect
The margins=True parameter adds totals for rows and columns in the crosstab.
What does normalize='index' do in a crosstab?
AShows proportions across each row
BShows proportions across each column
CShows overall proportions
DRemoves missing values
✗ Incorrect
Normalizing by 'index' converts counts into proportions across each row.
How do you specify multiple aggregation functions in a crosstab?
Aaggfunc=None
Baggfunc='sum,mean'
Caggfunc='multiple'
Daggfunc=[np.sum, np.mean]
✗ Incorrect
You pass a list of functions like [np.sum, np.mean] to the aggfunc parameter.
To create a multi-level index in a crosstab, you should:
AUse margins=True
BSet normalize='columns'
CPass lists of columns to index and columns parameters
DUse dropna=True
✗ Incorrect
Passing multiple columns as lists to index or columns creates multi-level indexes.
Which of these is NOT a valid use of pd.crosstab()?
ACreating a scatter plot
BAggregating numerical data with multiple functions
CCalculating frequency counts between two variables
DNormalizing counts to proportions
✗ Incorrect
Cross-tabulation does not create scatter plots; it summarizes categorical data.
Describe how to use pandas crosstab to analyze the relationship between three categorical variables with totals and normalized proportions.
Think about multi-index and normalization options in crosstab.
You got /3 concepts.
Explain how to apply multiple aggregation functions on numerical data within a pandas crosstab.
Consider how aggfunc and values work together.
You got /3 concepts.
Practice
(1/5)
1. What does the pd.crosstab() function primarily do in pandas?
easy
A. It sorts a DataFrame by multiple columns.
B. It computes a frequency table of two or more factors.
C. It merges two DataFrames based on a key column.
D. It filters rows based on a condition.
Solution
Step 1: Understand the purpose of pd.crosstab()
pd.crosstab() is used to compute a cross-tabulation, which is a frequency table showing counts of combinations of factors.
Step 2: Compare with other pandas functions
Sorting, merging, and filtering are done by other functions like sort_values(), merge(), and boolean indexing, not crosstab.
Final Answer:
It computes a frequency table of two or more factors. -> Option B
Quick Check:
Cross-tabulation = frequency table [OK]
Hint: Remember: crosstab counts combinations of categories [OK]
Common Mistakes:
Confusing crosstab with merge or join operations
Thinking crosstab sorts data
Assuming crosstab filters rows
2. Which of the following is the correct syntax to create a cross-tabulation with totals (margins) included?
easy
A. pd.crosstab(df['A'], df['B'], include_margins=True)
B. pd.crosstab(df['A'], df['B'], total=True)
C. pd.crosstab(df['A'], df['B'], add_totals=True)
D. pd.crosstab(df['A'], df['B'], margins=True)
Solution
Step 1: Recall the parameter for totals in pd.crosstab()
The correct parameter to add row and column totals is margins=True.
Step 2: Check other options for correctness
Parameters like total, add_totals, or include_margins do not exist in pd.crosstab.
Final Answer:
pd.crosstab(df['A'], df['B'], margins=True) -> Option D
Quick Check:
margins=True adds totals [OK]
Hint: Use margins=True to add totals in crosstab [OK]
Step 1: Group sales by City and Category with sum aggregation
For LA and A: sales = 200; LA and B: 300; NY and A: 100 + 250 = 350; NY and B: 150.
Step 2: Check the options carefully
{'A': {'LA': 200, 'NY': 250}, 'B': {'LA': 300, 'NY': 150}} matches the sums except NY and A is 250 in D, but actual sum is 100 + 250 = 350, so re-check.
Step 3: Recalculate sums
NY & A: rows 0 and 4 have sales 100 and 250, total 350. So correct sums are: LA A=200, LA B=300, NY A=350, NY B=150.
A. The 'values' parameter must be numeric, but 'Sales' contains strings.
B. The 'aggfunc' parameter must be a string, not a function.
C. The 'margins' parameter cannot be used with 'aggfunc'.
D. The DataFrame 'df' is missing the 'Category' column.
Solution
Step 1: Understand the interaction of margins and aggfunc
Using margins=True with aggfunc other than 'count' or 'sum' can cause errors because totals for means are ambiguous.
Step 2: Check other options for errors
Sales is numeric, so A is wrong; aggfunc='mean' is a valid string, so B is wrong; if 'Category' was missing, it would raise KeyError, not TypeError, so D is wrong.
Final Answer:
The 'margins' parameter cannot be used with 'aggfunc'. -> Option C
Quick Check:
margins with aggfunc='mean' causes error [OK]
Hint: Avoid margins=True with aggfunc='mean' to prevent errors [OK]
Common Mistakes:
Assuming margins always works with aggfunc
Confusing TypeError with KeyError
Thinking 'values' must be strings
5. You have a DataFrame with columns Region, Product, and Profit. You want to create a cross-tabulation that shows the average profit per product in each region, including overall averages per region and product. Which code snippet correctly achieves this?
hard
A. pd.crosstab(df['Region'], df['Product'], values=df['Profit'], aggfunc='mean', margins=True)
B. pd.crosstab(df['Region'], df['Product'], values=df['Profit'], aggfunc='sum', margins=True)
C. pd.crosstab(df['Region'], df['Product'], values=df['Profit'], aggfunc='mean')
D. pd.crosstab(df['Region'], df['Product'], aggfunc='mean', margins=True)
Solution
Step 1: Identify the need for average profit with totals
We want average profit per product per region, plus overall averages (totals), so aggfunc='mean' and margins=True are needed.
Step 2: Check the use of values parameter
To aggregate profit, values=df['Profit'] must be specified; otherwise, crosstab counts occurrences.