Chapter 1 · Section 4 practice
Pandas Series
Work from question 1 to 10. Edit your Python box, click Run Code, review the result, then click Submit attempt. Reference answers unlock after all ten attempts are submitted. This records completion, not correctness. Each box runs independently. Progress is saved in this browser.
1. Create labels
Warm-up. Create a Series of [100, 120, 110] labelled Mon, Tue, Wed, and print it.
Write your answer, then run it.
2. Select a label
Warm-up. Print Tue using loc and the first observation using iloc.
Write your answer, then run it.
3. Summarize totals
Warm-up. Print the sum and mean of this Series.
Write your answer, then run it.
4. Interpret describe
Build your skills. Use describe to print its count and 50% values. Explain 50% in a comment.
Write your answer, then run it.
5. Select high days
Build your skills. Print only values above 110, keeping their day labels.
Write your answer, then run it.
6. Rank days
Build your skills. Print the Series in descending order and the label of its largest value.
Write your answer, then run it.
7. Count order types
Build your skills. Print value_counts for Tea/Coffee orders, then print the Tea count alone.
Write your answer, then run it.
8. Match standard deviations
Challenge. Print pandas default std and NumPy std with a matching ddof, both to three decimals.
Write your answer, then run it.
9. Align dish labels
Challenge. Calculate count × bill with the Series below. Print the result and explain why differently ordered labels still pair correctly.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Create labels
import pandas as pd
sales = pd.Series([100, 120, 110], index=["Mon", "Tue", "Wed"])
print(sales)Expected output
Mon 100
Tue 120
Wed 110
dtype: int64The index identifies each observation.
2. Select a label
import pandas as pd
sales = pd.Series([100, 120, 110], index=["Mon", "Tue", "Wed"])
print(sales.loc["Tue"])
print(sales.iloc[0])Expected output
120
100loc["Tue"] selects the Tue label. iloc[0] selects the first observation because numerical positions start at 0.
3. Summarize totals
import pandas as pd
sales = pd.Series([100, 120, 110])
print(sales.sum())
print(sales.mean())Expected output
330
110.0330 is the total; 110 is the daily average.
4. Interpret describe
import pandas as pd
sales = pd.Series([100, 120, 110])
summary = sales.describe()
print(summary.loc["count"])
print(summary.loc["50%"])
# 50% is the median, not half the sum.Expected output
3.0
110.0The median of the sorted observations is 110.
5. Select high days
import pandas as pd
sales = pd.Series([100, 120, 110], index=["Mon", "Tue", "Wed"])
print(sales[sales > 110])Expected output
Tue 120
dtype: int64A Boolean condition preserves the selected labels.
6. Rank days
import pandas as pd
sales = pd.Series([100, 120, 110], index=["Mon", "Tue", "Wed"])
print(sales.sort_values(ascending=False))
print(sales.idxmax())Expected output
Tue 120
Wed 110
Mon 100
dtype: int64
Tueidxmax identifies the label, not the numerical maximum.
7. Count order types
import pandas as pd
orders = pd.Series(["Tea", "Coffee", "Tea", "Tea", "Coffee"])
counts = orders.value_counts()
print(counts)
print(counts.loc["Tea"])Expected output
Tea 3
Coffee 2
Name: count, dtype: int64
3The category index lets you select one frequency.
8. Match standard deviations
import numpy as np
import pandas as pd
sales = pd.Series([100, 102, 98, 105, 101])
print(f"{sales.std():.3f}")
print(f"{np.std(sales.to_numpy(), ddof=1):.3f}")Expected output
2.588
2.588Both calculations use the sample denominator.
9. Align dish labels
import pandas as pd
counts = pd.Series([85, 110], index=["Wonton", "Char Siu"])
bills = pd.Series([58, 42], index=["Char Siu", "Wonton"])
print(counts * bills)
# Pandas matches dish labels rather than array positions.Expected output
Char Siu 6380
Wonton 3570
dtype: int64Char Siu is 110×58; Wonton is 85×42.
10. Interpret revenue shares
import pandas as pd
revenue = pd.Series([3570, 6380, 3920], index=["Wonton", "Char Siu", "Milk Tea"])
shares = revenue / revenue.sum() * 100
print(shares.round(1))
# Costs are missing, so revenue share is not profit share.Expected output
Wonton 25.7
Char Siu 46.0
Milk Tea 28.3
dtype: float64Each dish is divided by total revenue. The rounded shares need not sum to exactly 100.