0
0
Pandasdata~10 mins

Cross-tabulation advanced usage 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 simple cross-tabulation of 'Gender' and 'Preference'.

Pandas
import pandas as pd

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

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

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

Pandas
ct = pd.crosstab(df['Gender'], df['Preference'], [1]=True)
print(ct)
Drag options to blanks, or click blank then click option'
Anormalize
Bmargins
Cdropna
Daggfunc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'normalize' instead of 'margins'.
Using 'aggfunc' without specifying a function.
3fill in blank
hard

Fix the error in the code to normalize the cross-tabulation by columns.

Pandas
ct = pd.crosstab(df['Gender'], df['Preference'], normalize=[1])
print(ct)
Drag options to blanks, or click blank then click option'
A'columns'
BTrue
C'index'
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using True instead of 'columns' for normalization.
Using 'index' which normalizes by rows.
4fill in blank
hard

Fill both blanks to create a cross-tabulation with aggregation of mean 'Score' by 'Gender' and 'Preference'.

Pandas
data = {'Gender': ['Male', 'Female', 'Female', 'Male'], 'Preference': ['A', 'B', 'A', 'B'], 'Score': [10, 20, 15, 25]}
df = pd.DataFrame(data)

ct = pd.crosstab(df['Gender'], df['Preference'], values=df[[1]], aggfunc=[2])
print(ct)
Drag options to blanks, or click blank then click option'
A'Score'
B'Gender'
Csum
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-numeric column for values.
Using 'sum' instead of 'mean' for average.
5fill in blank
hard

Fill all three blanks to create a normalized cross-tabulation with margins, aggregating the sum of 'Score' by 'Gender' and 'Preference'.

Pandas
ct = pd.crosstab(df[[1]], df[[2]], values=df['Score'], aggfunc=[3], normalize='all', margins=True)
print(ct)
Drag options to blanks, or click blank then click option'
A'Gender'
B'Preference'
Csum
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mean' instead of 'sum' for aggregation.
Swapping the row and column variables.