Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The crosstab function takes two series. Here, we want to cross-tabulate 'Gender' with 'Preference', so the second argument must be df['Preference'].
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'normalize' instead of 'margins'.
Forgetting to set the parameter to True.
✗ Incorrect
The 'margins' parameter adds row and column totals to the crosstab output when set to True.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using True or False instead of 'index' or 'columns'.
Using 'columns' when row normalization is needed.
✗ Incorrect
To normalize by row, the 'normalize' parameter should be set to 'index'.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the row and column variables.
Using column names not present in the DataFrame.
✗ Incorrect
The first blank is the row variable 'Gender', and the second blank is the column variable 'Preference'.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting margins to False when totals are needed.
Swapping row and column variables.
Not normalizing by columns correctly.
✗ Incorrect
The first blank is 'Gender' for rows, the second is 'Preference' for columns, and margins should be True to show totals.