Cross-tabulation is a method to summarize the relationship between two or more categorical variables by showing their frequency distribution in a table format.
Click to reveal answer
beginner
What does the pandas function crosstab() do?
crosstab() creates a cross-tabulation table that counts the frequency of combinations of values from two or more categorical variables.
Click to reveal answer
beginner
How do you interpret the rows and columns in a crosstab table?
Rows represent categories of one variable, columns represent categories of another variable, and the cell values show how many times each combination occurs.
Click to reveal answer
intermediate
What parameter in crosstab() allows you to add margins (totals)?
The parameter margins=True adds row and column totals to the crosstab table.
Click to reveal answer
intermediate
How can you normalize a crosstab table to show proportions instead of counts?
Use the normalize parameter in crosstab(). For example, normalize='index' shows row-wise proportions.
Click to reveal answer
What type of variables is crosstab() mainly used for?
AText variables
BContinuous variables
CCategorical variables
DDatetime variables
✗ Incorrect
crosstab() is designed to analyze categorical variables by counting combinations of their categories.
Which pandas function creates a frequency table for two categorical variables?
Acrosstab()
Bgroupby()
Cpivot_table()
Dmerge()
✗ Incorrect
crosstab() is the function that creates cross-tabulation frequency tables.
What does setting margins=True do in crosstab()?
AAdds totals for rows and columns
BRemoves missing values
CSorts the table alphabetically
DNormalizes the data
✗ Incorrect
It adds a row and column showing the total counts.
How can you get proportions instead of counts in a crosstab table?
ASet <code>normalize=True</code>
BSet <code>normalize='all'</code>
CSet <code>normalize=False</code>
DSet <code>normalize='index'</code> or <code>normalize='columns'</code>
✗ Incorrect
Using normalize='index' or normalize='columns' shows proportions by row or column.
If you want to analyze the relationship between gender and purchase decision, which function is best?
Amean()
Bcrosstab()
Csum()
Ddescribe()
✗ Incorrect
crosstab() helps summarize how gender and purchase decision categories relate.
Explain what cross-tabulation is and how crosstab() helps in data analysis.
Think about how you compare two groups in a simple table.
You got /4 concepts.
Describe how to add totals and normalize data in a crosstab table using pandas.
Consider how to see overall counts and proportions.
You got /3 concepts.
Practice
(1/5)
1. What does the pd.crosstab() function do in data analysis?
easy
A. It sorts data in ascending order.
B. It counts how often categories from two variables appear together.
C. It calculates the average of numerical columns.
D. It removes missing values from the dataset.
Solution
Step 1: Understand the purpose of crosstab()
The function pd.crosstab() is used to count occurrences of combinations of categories from two variables.
Step 2: Compare with other options
Sorting, averaging, and removing missing values are different functions, not related to crosstab.
Final Answer:
It counts how often categories from two variables appear together. -> Option B
Quick Check:
Cross-tabulation = counts of category pairs [OK]
Hint: Remember: crosstab counts category pairs, not calculations [OK]
Common Mistakes:
Thinking crosstab sorts data
Confusing crosstab with aggregation functions
Assuming crosstab cleans data
2. Which of the following is the correct syntax to create a cross-tabulation table using pandas?
easy
A. pd.crosstab(index=df['AgeGroup'], columns=df['Gender'])
B. pd.crosstab(df.Gender, columns='AgeGroup')
C. pd.crosstab(columns=df['Gender'], index=df['AgeGroup'])
D. pd.crosstab(df['Gender'], df['AgeGroup'])
Solution
Step 1: Recall the correct parameter order for crosstab()
The function pd.crosstab() takes the first argument as the row variable and the second as the column variable, both as Series or arrays.
Step 2: Check each option
pd.crosstab(df['Gender'], df['AgeGroup']) correctly passes two Series without naming parameters. pd.crosstab(index=df['AgeGroup'], columns=df['Gender']) uses named parameters with row and column variables reversed. pd.crosstab(df.Gender, columns='AgeGroup') passes a string literal instead of a Series for the columns parameter. pd.crosstab(columns=df['Gender'], index=df['AgeGroup']) swaps index and columns incorrectly.
Final Answer:
pd.crosstab(df['Gender'], df['AgeGroup']) -> Option D
Quick Check:
Use positional args: rows, columns [OK]
Hint: Use positional args: first is rows, second is columns [OK]
A. The DataFrame is missing a column named 'Result'.
B. The index parameter should be a list, not a Series.
C. The columns parameter should be a Series, not a string.
D. The crosstab function requires numeric data only.
Solution
Step 1: Check the columns argument in crosstab()
The columns argument must be a Series or array representing categories, not a string literal.
Step 2: Identify the error in the code
Here, columns='Result' is a string, not df['Result']. This causes crosstab to treat it incorrectly.
Final Answer:
The columns parameter should be a Series, not a string. -> Option C
Quick Check:
Columns must be Series, not string [OK]
Hint: Pass Series for index and columns, not strings [OK]
Common Mistakes:
Passing column name as string instead of Series
Assuming crosstab needs numeric data
Confusing index and columns parameters
5. You have a DataFrame with columns 'Department' and 'Satisfaction' (values: 'High', 'Medium', 'Low'). You want to find how many employees in each department have each satisfaction level, but only show departments with at least 3 employees total. Which code correctly does this?
hard
A. ct = pd.crosstab(df['Department'], df['Satisfaction'])
ct = ct[ct.sum(axis=1) >= 3]
B. ct = pd.crosstab(index=df['Satisfaction'], columns=df['Department'])
ct = ct[ct.sum(axis=0) >= 3]
C. ct = pd.crosstab(df['Department'], df['Satisfaction'])
ct = ct[ct.sum(axis=0) >= 3]
D. ct = pd.crosstab(index=df['Department'], columns=df['Satisfaction'])
ct = ct[ct.sum(axis=1) < 3]
Solution
Step 1: Create crosstab with Department as rows and Satisfaction as columns
Use pd.crosstab(df['Department'], df['Satisfaction']) to count satisfaction levels per department.
Step 2: Filter departments with total employees >= 3
Sum counts across columns (axis=1) for each department, then keep rows where sum >= 3.