0
0
Data Analysis Pythondata~30 mins

Cross-tabulation with crosstab() in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Cross-tabulation with crosstab()
📖 Scenario: You work in a small bookstore. You have data about customers' favorite book genres and their preferred reading time (Morning, Afternoon, Evening). You want to understand how many customers prefer each genre at different times of the day.
🎯 Goal: Create a cross-tabulation table using pandas.crosstab() to show the count of customers for each combination of Genre and ReadingTime.
📋 What You'll Learn
Create a pandas DataFrame with the exact data provided.
Create a variable for the column names to use in crosstab.
Use pandas.crosstab() with the specified columns.
Print the resulting cross-tabulation table.
💡 Why This Matters
🌍 Real World
Cross-tabulation helps businesses understand relationships between categories, like customer preferences by time or location.
💼 Career
Data analysts and scientists use crosstab to summarize and explore categorical data quickly for reports and decision-making.
Progress0 / 4 steps
1
Create the customer data DataFrame
Create a pandas DataFrame called df with these exact columns and values:
CustomerID: [1, 2, 3, 4, 5, 6, 7, 8]
Genre: ['Fiction', 'Non-Fiction', 'Fiction', 'Sci-Fi', 'Non-Fiction', 'Sci-Fi', 'Fiction', 'Sci-Fi']
ReadingTime: ['Morning', 'Afternoon', 'Evening', 'Morning', 'Evening', 'Afternoon', 'Morning', 'Evening']
Data Analysis Python
Hint

Use pd.DataFrame() with a dictionary where keys are column names and values are lists of data.

2
Set the columns for crosstab
Create a list called columns with the exact values ['Genre', 'ReadingTime'] to specify the columns to use in the crosstab.
Data Analysis Python
Hint

Just assign the list ['Genre', 'ReadingTime'] to the variable columns.

3
Create the cross-tabulation table
Use pandas.crosstab() with df[columns[0]] as rows and df[columns[1]] as columns. Store the result in a variable called cross_tab.
Data Analysis Python
Hint

Use pd.crosstab(df[columns[0]], df[columns[1]]) to create the table.

4
Print the cross-tabulation table
Print the variable cross_tab to display the cross-tabulation table.
Data Analysis Python
Hint

Use print(cross_tab) to show the table.