0
0
Data Analysis Pythondata~10 mins

Cross-tabulation with crosstab() in Data Analysis Python - 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 from the DataFrame df using columns 'Gender' and 'Preference'.

Data Analysis Python
import pandas as pd

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

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

Data Analysis Python
ct = pd.crosstab(df['Gender'], df['Preference'], [1]=True)
print(ct)
Drag options to blanks, or click blank then click option'
Amargins
Bnormalize
Cdropna
Daggfunc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'normalize' instead of 'margins'.
Forgetting to set the parameter to True.
3fill in blank
hard

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

Data Analysis Python
ct = pd.crosstab(df['Gender'], df['Preference'], margins=True, normalize=[1])
print(ct)
Drag options to blanks, or click blank then click option'
AFalse
B'columns'
C'index'
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using True or False instead of 'index' or 'columns'.
Using 'columns' when row normalization is needed.
4fill in blank
hard

Fill both blanks to create a cross-tabulation table with counts and rename the index and columns.

Data Analysis Python
ct = pd.crosstab(df[[1]], df[[2]], margins=True)
ct.index.name = 'Gender'
ct.columns.name = 'Preference'
print(ct)
Drag options to blanks, or click blank then click option'
A'Gender'
B'Age'
C'Preference'
D'City'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the row and column variables.
Using column names not present in the DataFrame.
5fill in blank
hard

Fill all three blanks to create a normalized cross-tabulation table by columns with margins and rename the index and columns.

Data Analysis Python
ct = pd.crosstab(df[[1]], df[[2]], margins=[3], normalize='columns')
ct.index.name = 'Gender'
ct.columns.name = 'Preference'
print(ct)
Drag options to blanks, or click blank then click option'
A'Gender'
B'Preference'
CTrue
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Setting margins to False when totals are needed.
Swapping row and column variables.
Not normalizing by columns correctly.