Build a row-normalized gender by education crosstab in pandas
Quick Overview
Write a pandas function that cross-tabulates gender against education and turns the counts into proportions that sum to one within each gender. It tests row normalization, axis alignment when dividing by totals, zero-filled combinations and checking float sums.
Build a row-normalized gender by education crosstab in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `crosstab_row_normalized(df)`, which cross-tabulates `'gender'` (rows) against `'education'` (columns) and divides each cell by its row total, so each row holds the share of that gender's rows in each education level and sums to `1.0`. Return the resulting DataFrame as-is.
The returned table must:
- hold `float64` values, with each row summing to `1.0` (up to floating-point rounding);
- keep the axis names `'gender'` (index) and `'education'` (columns);
- keep gender as the index (no `reset_index()`).
For example:
```python
df = pd.DataFrame({
'gender': ['M', 'M', 'F', 'F'],
'education': ['HS', 'BA', 'HS', 'BA'],
})
crosstab_row_normalized(df)
# education BA HS
# gender
# F 0.5 0.5
# M 0.5 0.5
```
```hint Divide along the right axis
Count the pairs first, then divide each cell by its row's total. If you do the division yourself, check whether pandas lines the totals up with the rows or with the columns.
```
### What a Strong Answer Covers
- A function that returns proportions with gender on the index, education on the columns, the required axis names and `float64` cells.
- What row normalization means for reading the table, and how it differs from normalizing by column or by the grand total.
- How combinations that never occur and rows with missing values affect the proportions.
- How to check the row sums, given floating-point rounding.
### Follow-up Questions
- How would you normalize over columns or the whole table instead?
- How are missing category combinations handled in the result?
- How would you get the same table from raw counts without the normalize argument?
- When would column normalization answer the business question better than row normalization?
Overview: Write a pandas function that cross-tabulates gender against education and turns the counts into proportions that sum to one within each gender. It tests row normalization, axis alignment when dividing by totals, zero-filled combinations and checking float sums.
Implement crosstab_row_normalized(df), which cross-tabulates 'gender' (rows) against 'education' (columns) and divides each cell by its row total, so each row holds the share of that gender's rows in each education level and sums to 1.0. Return the resulting DataFrame as-is.
The returned table must:
hold
float64
values, with each row summing to
1.0
(up to floating-point rounding);
keep the axis names
'gender'
(index) and
'education'
(columns);
keep gender as the index (no
reset_index()
).
For example:
df = pd.DataFrame({
'gender': ['M', 'M', 'F', 'F'],
'education': ['HS', 'BA', 'HS', 'BA'],
})
crosstab_row_normalized(df)
# education BA HS
# gender
# F 0.5 0.5
# M 0.5 0.5
What a Strong Answer Covers Guidance
A function that returns proportions with gender on the index, education on the columns, the required axis names and
float64
cells.
What row normalization means for reading the table, and how it differs from normalizing by column or by the grand total.
How combinations that never occur and rows with missing values affect the proportions.
How to check the row sums, given floating-point rounding.
Follow-up Questions Guidance
How would you normalize over columns or the whole table instead?
How are missing category combinations handled in the result?
How would you get the same table from raw counts without the normalize argument?
When would column normalization answer the business question better than row normalization?