On this page
Session 2 · Functions, filters and missing data
Exam focus. Write a function, apply it to a column or rows, select data using labels or positions, and justify your missing-value decision.
What to know
Use a function with explicit inputs and a return value. Use .loc for labels and .iloc for integer positions. Label slices include the endpoint when the endpoint label is present. Position slices exclude the stop position.
Code pattern
def add_tax(amount, rate=0.20):
return amount * (1 + rate)
df["Price"] = df["Price"].fillna(df["Price"].median())
selection = df.loc[
(df["Quantity"] > 30) & (df["Price"] < 50),
["Quantity", "Price"]
]
Exam traps
Parenthesize each condition before combining with & or |. Use the median for skewed numerical values when imputation makes sense, or the mode for categorical values. Dropping rows can bias a result when missingness is concentrated in one group.
Write an interpretation
State the result, explain what produced it, and say why it matters for the decision in the case.
Source: Data Handling Final Revision File and the exam announcement and mock supplied in chat.
Exam methods and worked application
Functions and row-level rules
def score(spend, visits, satisfaction, bonus=10):
base = spend / 10 + 2 * visits
return base + bonus if satisfaction >= 8 else base
df["Satisfaction"] = df["Satisfaction"].fillna(df["Satisfaction"].median())
df["Score"] = df.apply(
lambda row: score(row["Spend"], row["Visits"], row["Satisfaction"]),
axis=1,
)
eligible = (df["Score"] >= 50) & (df["Region"] == "Europe")
result = df.loc[eligible, ["Customer_ID", "Score"]]
A default parameter applies unless overwritten. A function can return multiple values, which the caller can unpack. For spend 400, visits 4 and satisfaction 9, the score is 58. axis=1 gives the function a row. Use | for OR and ~ to negate a mask.
Select and justify
| Method | Use | Main risk |
|---|---|---|
fillna(mean) | Numeric values without influential extremes | Pulls values toward the mean and can reduce dispersion |
fillna(median) | Skewed numeric values | Changes the distribution and can hide systematic missingness |
fillna(mode) | A defensible frequent category | Check ties or an entirely missing column before selecting a mode |
dropna(subset=["Price"]) | Price is essential to this specific analysis | Excluded rows may differ systematically |
dropna(axis=1, thresh=n) | Keep only columns with at least n valid entries | May remove important fields |
Missing satisfaction concentrated among unhappy customers can bias estimates upward if those rows are discarded. Compare missingness by group before deciding. Remember that len(series) includes missing positions and series.count() counts valid values.
Official guide · Parameters, scope and filtering
Practice defining and calling functions with parameters and return values. A parameter or variable assigned inside a function is local by default. The caller receives the return value. Indent the function body and any conditional block, and test at the boundary of each condition. Use masks to filter and a justified method for missing values.
Official Exam Preparation Guidelines (pp. 1–3).