4.6.3. Homework#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
1. Series Labels
A regional sales Series can use store names as index labels instead of only integer positions.
2. DataFrame Column Types
Every column in a pandas DataFrame must use the same data type as every other column.
3. Previewing Data
Calling head() is mainly used to preview rows; it does not clean or transform the DataFrame by itself.
4. Missing Data Checks
A manager can use isna() to count incomplete values before deciding whether to drop or fill them.
5. Grouped Summaries
A GroupBy operation can summarize revenue by region only after the data has been converted to a Python list.
### 6. Create a Customer Table
# Import pandas as pd, then create a DataFrame from names and purchases.
# Store the DataFrame in a variable named customers.
# Print exactly:
# Customer rows: 3
# Purchase total: 12
names = ["Ana", "Bo", "Cy"]
purchases = [5, 3, 4]
### Your code starts here.
### Your code ends here.
Customer rows: 3
Purchase total: 12
### 7. Average Order Value
# Import pandas as pd, then create a Series from orders.
# Calculate the average order value with mean().
# Print exactly:
# Average order: 35.0
orders = [25, 40, 35, 40]
### Your code starts here.
### Your code ends here.
Average order: 35.0
### 8. Select Region Values
# Import pandas as pd, then create a DataFrame with region and revenue columns.
# Select the West revenue and the first region from the DataFrame.
# Print exactly:
# West revenue: 900
# First region: East
rows = [["East", 1200], ["West", 900], ["South", 700]]
### Your code starts here.
### Your code ends here.
West revenue: 900
First region: East
### 9. Fill Missing Ratings
# Import pandas as pd, then create a Series from ratings.
# Count missing values with isna(), fill missing values with 0, and total the filled Series.
# Print exactly:
# Missing ratings: 1
# Rating total: 8
ratings = [5, None, 3]
### Your code starts here.
### Your code ends here.
Missing ratings: 1
Rating total: 8
### 10. Match Product Rows
# Import pandas as pd, then create a DataFrame with product and price columns.
# Filter rows where product is "pen" and print the number of matched rows and the pen price.
# Print exactly:
# Matched rows: 2
# Pen price: 2.5
products = [["pen", 2.5], ["notebook", 4.0], ["pen", 2.5]]
### Your code starts here.
### Your code ends here.
Matched rows: 2
Pen price: 2.5