0
0
Pandasdata~10 mins

crosstab() for cross-tabulation in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a cross-tabulation table between 'Gender' and 'Preference'.

Pandas
import pandas as pd

data = {'Gender': ['Male', 'Female', 'Female', 'Male'], 'Preference': ['A', 'B', 'A', 'B']}
df = pd.DataFrame(data)

result = pd.crosstab(df['Gender'], df[[1]])
print(result)
Drag options to blanks, or click blank then click option'
A'Preference'
B'Gender'
C'Age'
D'Score'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same column for both arguments.
Using a column name that does not exist in the DataFrame.
2fill in blank
medium

Complete the code to add margins (totals) to the cross-tabulation table.

Pandas
import pandas as pd

data = {'Team': ['A', 'B', 'A', 'B'], 'Result': ['Win', 'Lose', 'Lose', 'Win']}
df = pd.DataFrame(data)

ct = pd.crosstab(df['Team'], df['Result'], [1]=True)
print(ct)
Drag options to blanks, or click blank then click option'
Amargins
Btotals
Cadd_totals
Dinclude_margins
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like 'totals' or 'include_margins'.
Forgetting to set the parameter to True.
3fill in blank
hard

Fix the error in the code to correctly compute the cross-tabulation of 'City' and 'Category'.

Pandas
import pandas as pd

data = {'City': ['NY', 'LA', 'NY', 'LA'], 'Category': ['Food', 'Food', 'Tech', 'Tech']}
df = pd.DataFrame(data)

ct = pd.crosstab(df.City, df.[1])
print(ct)
Drag options to blanks, or click blank then click option'
ACategories
B'Category'
Ccategory
DCategory
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes inside attribute access like df.'Category'.
Using wrong capitalization for column names.
4fill in blank
hard

Fill both blanks to create a normalized cross-tabulation table showing row proportions.

Pandas
import pandas as pd

data = {'Group': ['X', 'Y', 'X', 'Y'], 'Outcome': ['Pass', 'Fail', 'Fail', 'Pass']}
df = pd.DataFrame(data)

ct = pd.crosstab(df[[1]], df[[2]], normalize='index')
print(ct)
Drag options to blanks, or click blank then click option'
A'Group'
B'Outcome'
C'Pass'
D'Fail'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the row and column variables.
Using values instead of column names.
5fill in blank
hard

Fill all three blanks to create a cross-tabulation table with margins and specify the aggregation function to count unique 'UserID's.

Pandas
import pandas as pd

data = {'UserID': [1, 2, 1, 3, 2], 'Product': ['A', 'A', 'B', 'B', 'A'], 'Region': ['East', 'West', 'East', 'West', 'East']}
df = pd.DataFrame(data)

ct = pd.crosstab(df[[1]], df[[2]], values=df[[3]], aggfunc='nunique', margins=True)
print(ct)
Drag options to blanks, or click blank then click option'
A'Region'
B'Product'
C'UserID'
D'User'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect column names or values.
Forgetting to set margins=True.