Homework

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.

Hide code cell source

### Solution
# True
# Series objects support labeled indexes, which make business values easier to identify.

2. DataFrame Column Types

Every column in a pandas DataFrame must use the same data type as every other column.

Hide code cell source

### Solution
# False
# A DataFrame can hold different dtypes in different columns.

3. Previewing Data

Calling head() is mainly used to preview rows; it does not clean or transform the DataFrame by itself.

Hide code cell source

### Solution
# True
# `head()` displays rows so you can inspect a table before analysis.

4. Missing Data Checks

A manager can use isna() to count incomplete values before deciding whether to drop or fill them.

Hide code cell source

### Solution
# True
# `isna()` returns a missing-value mask that can be counted or inspected.

5. Grouped Summaries

A GroupBy operation can summarize revenue by region only after the data has been converted to a Python list.

Hide code cell source

### Solution
# False
# GroupBy works directly on pandas Series and DataFrame objects.
### 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.

Hide code cell source

### Solution
import pandas as pd

names = ["Ana", "Bo", "Cy"]
purchases = [5, 3, 4]

customers = pd.DataFrame({"name": names, "purchases": purchases})

print("Customer rows:", len(customers))
print("Purchase total:", int(customers["purchases"].sum()))
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.

Hide code cell source

### Solution
import pandas as pd

orders = [25, 40, 35, 40]

order_series = pd.Series(orders)
average_order = order_series.mean()

print("Average order:", average_order)
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.

Hide code cell source

### Solution
import pandas as pd

rows = [["East", 1200], ["West", 900], ["South", 700]]

revenue_df = pd.DataFrame(rows, columns=["region", "revenue"])
west_revenue = revenue_df.loc[revenue_df["region"] == "West", "revenue"].iloc[0]
first_region = revenue_df.iloc[0]["region"]

print("West revenue:", int(west_revenue))
print("First region:", first_region)
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.

Hide code cell source

### Solution
import pandas as pd

ratings = [5, None, 3]

rating_series = pd.Series(ratings)
missing_ratings = int(rating_series.isna().sum())
filled_ratings = rating_series.fillna(0)

print("Missing ratings:", missing_ratings)
print("Rating total:", int(filled_ratings.sum()))
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.

Hide code cell source

### Solution
import pandas as pd

products = [["pen", 2.5], ["notebook", 4.0], ["pen", 2.5]]

products_df = pd.DataFrame(products, columns=["product", "price"])
pen_rows = products_df[products_df["product"] == "pen"]
pen_price = pen_rows.iloc[0]["price"]

print("Matched rows:", len(pen_rows))
print("Pen price:", pen_price)
Matched rows: 2
Pen price: 2.5