Chapter 2 · Section 1 practice
DataFrames and the First Look
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. Table dimensions
Warm-up. Print the number of rows and columns separately.
Write your answer, then run it.
2. Column labels
Warm-up. Print the column labels as a Python list.
Write your answer, then run it.
3. First observations
Warm-up. Print the first two rows.
Write your answer, then run it.
4. Last observation
Build your skills. Print the final row.
Write your answer, then run it.
5. A column type
Build your skills. Print the dtype of the Units column.
Write your answer, then run it.
6. Series or table
Build your skills. Print the type names of df[“Units”] and df[[“Units”]].
Write your answer, then run it.
7. Named row labels
Build your skills. Print the index labels as a list. Explain how they differ from positions 0–3.
Write your answer, then run it.
8. Numeric text
Challenge. Convert SalesText into a numerical column and print its total.
Write your answer, then run it.
9. Load the course CSV
Challenge. Load the retail CSV using OrderID as its index. Print its shape and first index label.
Write your answer, then run it.
10. Inspect before calculating
Challenge. Create a two-row DataFrame with Store and Sales columns. Print its dimensions and total sales. Use stores Central/Kowloon and sales 120/80.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Table dimensions
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.shape[0])
print(df.shape[1])Expected output
4
3The table contains four observations and three variables.
2. Column labels
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.columns))Expected output
['Store', 'Units', 'Price']The keys used to create the DataFrame become its column names.
3. First observations
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.head(2))Expected output
Store Units Price
A Central 2 120
B Kowloon 1 80head(2) returns two rows without changing df.
4. Last observation
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.tail(1))Expected output
Store Units Price
D Kowloon 3 60tail(1) is useful for checking the end of a file.
5. A column type
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"].dtype)Expected output
int64A selected Series has one dtype.
6. Series or table
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(type(df["Units"]).__name__)
print(type(df[["Units"]]).__name__)Expected output
Series
DataFrameA list of column names preserves a DataFrame, even for one column.
7. Named row labels
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.index))Expected output
['A', 'B', 'C', 'D']A, B, C, and D are labels; numerical positions describe ordering.
8. Numeric text
import pandas as pd
df = pd.DataFrame({"SalesText": ["120", "80", "35"]})
df["Sales"] = pd.to_numeric(df["SalesText"])
print(df["Sales"].sum())Expected output
235Text containing digits should be converted before numerical analysis.
9. Load the course CSV
import pandas as pd
url = "https://busanalytics-book.pages.dev/data/chapter2-retail-orders.csv"
df = pd.read_csv(url, index_col="OrderID")
print(df.shape)
print(df.index[0])Expected output
(8, 6)
O101The eight order lines have six remaining columns after OrderID becomes the index.
10. Inspect before calculating
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon"], "Sales": [120, 80]})
print(df.shape)
print(df["Sales"].sum())Expected output
(2, 2)
200You should establish what the columns represent before calculating a total.