Chapter 2 · Section 2 practice
Selecting Columns, Rows, and Cells
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. Select a Series
Warm-up. Print the Units column as a Series.
Write your answer, then run it.
2. Select two columns
Warm-up. Print a DataFrame containing Store and Price.
Write your answer, then run it.
3. A positional slice
Warm-up. Print labels selected by positions 1:3.
Write your answer, then run it.
4. A label slice
Build your skills. Print labels selected by B:D.
Write your answer, then run it.
5. A single cell
Build your skills. Print the price of row C using loc.
Write your answer, then run it.
6. Quantity threshold
Build your skills. Print the labels of orders with at least three units.
Write your answer, then run it.
7. Two conditions
Build your skills. Print Units and Price for Central orders with at least two units.
Write your answer, then run it.
8. Either condition
Challenge. Print the labels of orders with Price >= 100 OR Units >= 4.
Write your answer, then run it.
9. Membership and ranges
Challenge. Keep Central rows whose Units lie between 2 and 4 inclusive. Print their labels.
Write your answer, then run it.
10. Update a separate result
Challenge. Copy df, reduce prices by 10 for Kowloon rows, then print the new prices and the original prices as lists.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Select a Series
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
print(df["Units"])Expected output
A 2
B 1
C 4
D 3
Name: Units, dtype: int64Single-column selection gives a Series.
2. Select two columns
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
print(df[["Store", "Price"]])Expected output
Store Price
A Central 120
B Kowloon 80
C Central 35
D Kowloon 60A list of names selects a smaller DataFrame.
3. A positional slice
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
print(list(df.iloc[1:3].index))Expected output
['B', 'C']The stop position is excluded.
4. A label slice
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
print(list(df.loc["B":"D"].index))Expected output
['B', 'C', 'D']Both endpoints are included in this ordered label slice.
5. A single cell
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
print(df.loc["C", "Price"])Expected output
35The row label and column name identify one value.
6. Quantity threshold
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
print(list(df[df["Units"] >= 3].index))Expected output
['C', 'D']A Boolean condition retains C and D.
7. Two conditions
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
mask = (df["Store"] == "Central") & (df["Units"] >= 2)
print(df.loc[mask, ["Units", "Price"]])Expected output
Units Price
A 2 120
C 4 35Both comparisons must hold, and parentheses make the mask unambiguous.
8. Either condition
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
mask = (df["Price"] >= 100) | (df["Units"] >= 4)
print(list(df[mask].index))Expected output
['A', 'C']The union keeps A and C.
9. Membership and ranges
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
mask = df["Store"].isin(["Central"]) & df["Units"].between(2, 4)
print(list(df[mask].index))Expected output
['A', 'C']Both isin and between return Boolean Series.
10. Update a separate result
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
changed = df.copy()
mask = changed["Store"] == "Kowloon"
changed.loc[mask, "Price"] = changed.loc[mask, "Price"] - 10
print(list(changed["Price"]))
print(list(df["Price"]))Expected output
[120, 70, 35, 50]
[120, 80, 35, 60]The explicit copy preserves the original table.