flowchart TD
subgraph RawTable["DataFrame Anatomy"]
DF["clinic_triage (120 x 11)<br/>Row Index: 0..119 | Columns: Series"]
end
subgraph Indexing["Indexing & Masks"]
LOC[".loc[row_lbl, col_lbl]<br/>Inclusive label slices + masks"]
ILOC[".iloc[row_pos, col_pos]<br/>Exclusive 0-based positions"]
MASK["Boolean Mask (&, |, ~, .isin)<br/>Filter -> .reset_index(drop=True)"]
end
subgraph Reshape["Tidy Reshaping"]
MELT["pd.melt (Wide -> Long)<br/>id_vars + var_name + value_name"]
XTAB["pd.crosstab / pivot_table<br/>Row x Column Summary Matrix"]
end
DF -->|"by label"| LOC
DF -->|"by position"| ILOC
DF -->|"row filter"| MASK
LOC -. "unpivot visits" .-> MELT
MASK -. "count groups" .-> XTAB
17 Pandas Foundations — Series, DataFrames, Indexing, Filtering, and Reshaping
Chapter Mission: Build your fluency with Pandas, the backbone of tabular data science in Python. Master
SeriesandDataFramearchitecture, explicit label vs. position indexing (.locvs..iloc), vectorized boolean masking (&,|,~,.isin()), mutation-safe Pandas 3.x Copy-on-Write transformations (.copy(),.assign()), deterministic multi-key sorting, and tidy data reshaping (pd.melt,pd.crosstab, andpivot_table) using a real bundled clinical triage dataset.
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch17 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch17 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch17 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this chapter, you will be able to:
- Distinguish a 1D labeled
Seriesfrom a 2D heterogeneousDataFrame, inspect schemas and summary statistics (including Pandas’ sample standard deviation default \(s\) withddof=1), and select subsets safely. - Choose deliberately between label-based indexing (
.loc, inclusive endpoints) and integer-position indexing (.iloc, exclusive stop), avoiding off-by-one and out-of-bounds traps. - Construct vectorized boolean masks with
&,|,~, and.isin(), resetting filtered indexes cleanly with.reset_index(drop=True). - Write pure, mutation-free Pandas 3.x transformations using
.copy()and.assign(), never relying oninplace=Trueor chained assignment (df[mask]["col"] = ...). - Reshape tabular research datasets fluently between wide and tidy long layouts using
pd.melt, and build zero-filled contingency and summary tables withpd.crosstaband.pivot_table().
17.1 Stage 1 — First-Principles Intuition: Why Tabular Research Needs Pandas
In Chapter 16, you saw how NumPy accelerates homogeneous numerical arrays—a matrix where every entry shares the exact same float64 type. Real clinical cohorts, economic surveys, and genomics metadata are almost never homogeneous: a single patient row mixes a string ID ("UHC-0001"), a categorical district ("Sylhet"), an integer age (55), and floating-point vitals (37.5 °C, 12.2 g/dL).
Pandas sits directly on top of NumPy to give each column its own typed NumPy/Arrow vector while binding every row and column to explicit, alignment-aware labels:
Series(1D Labeled Vector): One typed column paired with a rowIndex(for example,df["temp_c"]holds 120float64body temperatures).DataFrame(2D Labeled Table): An ordered dictionary ofSeriessharing one common rowIndex—each column can hold a different dtype (str,int64,float64,bool).- Vectorized Column Operations: Writing
df["weight_kg"] / (df["height_cm"] / 100) ** 2computes BMI across 120—or 12 million—rows in compiled C loops without a single Pythonforloop.
Think of a DataFrame as a bound hospital ledger where every row has a stamped chart label (Index) and every column has a printed header and unit:
.iloc[row_pos, col_pos]counts physical lines from the top of the page starting at0(like Python list slicing, the stop index is exclusive)..loc[row_label, col_label]looks up rows and columns by their printed labels (just like a dictionary, and slice endpoints are inclusive).- Wide vs. Long Format: Wide tables put repeated visits in separate columns (
sbp_day1,sbp_day2)—handy for paired differences (day2 - day1). Tidy long tables stack one measurement per row (patient_id,visit,sbp)—required by Seaborn, Statsmodels, and longitudinal mixed-effects models.
17.1.1 The Modern Pandas 3.x Copy-on-Write Discipline
Older Pandas tutorials are packed with inplace=True and chained indexing like df[df["age_years"] >= 18]["bmi"] = ..., which caused notorious SettingWithCopyWarning bugs. In Pandas 3.x, Copy-on-Write (CoW) is active by default: chained assignment fails or silently does nothing to df, and inplace=True returns None. Throughout this book, we follow one ironclad rule: never mutate inputs in place—always derive new tables via out = df.copy() or .assign(...), and return the new DataFrame explicitly.
17.2 Stage 2 — Visual Mental Model: Indexing & Wide-to-Long Reshaping
Trace how a single DataFrame routes through .loc/.iloc indexing, boolean filtering, and pd.melt / pd.crosstab reshaping without ever mutating the raw cohort table:
Keep two visual invariants anchored in your head as you code:
- Row filters preserve old index numbers (e.g.,
[3, 16, 28, ...]) until you explicitly call.reset_index(drop=True). - Reshaping changes table geometry, not data truth:
pd.meltstretches \(N\) patients \(\times\) \(K\) visits into \(N \cdot K\) tidy rows, whereaspd.crosstabcompresses individual rows into a compact district-by-priority count matrix.
By default, Pandas Series.std() computes the sample standard deviation with Bessel’s correction (ddof=1, dividing by \(n - 1\)), whereas NumPy np.std() defaults to the population standard deviation (ddof=0, dividing by \(n\)). Mixing the two in a statistical pipeline silently shifts every standard deviation and \(z\)-score!
17.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_04_data_science_math/ch17_pandas_foundations/01_walkthrough.ipynb in Google Colab and execute every cell alongside the dissections below.
17.3.1 Loading Data, Series vs. DataFrame, & Summaries
First, load the real bundled Bangladesh upazila health complex triage dataset via sharukh_tutor and inspect its shape, types, and summary statistics:
import pandas as pd
import sharukh_tutor as tutor
df = tutor.load_data("clinic_triage", as_pandas=True)
print("Shape:", df.shape)
print("DataFrame type:", type(df).__name__)
print("Series type: ", type(df["temp_c"]).__name__)
print("Mean temp_c: ", round(float(df["temp_c"].mean()), 2))
print("Median temp_c: ", round(float(df["temp_c"].median()), 2))
print("Std temp_c: ", round(float(df["temp_c"].std()), 2))Expected Output:
Notice the single-bracket versus double-bracket distinction: df["temp_c"] extracts a 1D Series, whereas df[["temp_c"]] returns a 1-column 2D DataFrame. Wrapping aggregated values with float(round(..., 2)) converts Pandas/NumPy scalars into clean native Python floats ready for reports or JSON logs.
17.3.2 Label vs. Position Indexing (.loc vs. .iloc)
Watch the exact difference between integer-position slicing (.iloc) and label-based slicing (.loc):
Expected Output:
Compare those two slices side by side:
.iloc[0:3, 0:3]stops before position3—returning 3 rows (0, 1, 2) and 3 columns (0, 1, 2), identical to standard Python list slicing..loc["UHC-0001":"UHC-0003", ...]includes both"UHC-0001"and"UHC-0003", because label slices in Pandas are closed (inclusive) intervals.
17.3.3 Boolean Masking with &, |, ~, and .isin()
To isolate clinical sub-cohorts without writing loops, build elementwise boolean Series masks and combine them with bitwise operators (& for AND, | for OR, ~ for NOT):
adult_fever = (df["age_years"] >= 18) & (df["temp_c"] >= 38.0)
target_dist = df["district"].isin(["Dhaka", "Sylhet"])
sub = df.loc[adult_fever & target_dist].reset_index(drop=True)
other_count = int((~target_dist).sum())
print("Adult fever rows (all districts):", int(adult_fever.sum()))
print("Adult fever in Dhaka/Sylhet: ", len(sub))
print("Outside Dhaka/Sylhet rows: ", other_count)
print(sub[["patient_id", "district", "age_years", "temp_c"]].head(3))Expected Output:
Trace three critical syntax habits here:
- Parentheses around every comparison: Operator precedence binds
&tighter than>=, so omitting parentheses around(df["age_years"] >= 18)raisesTypeError. .isin([...])replaces chained|checks: Checking membership across 2 or 20 districts stays concise and fast..reset_index(drop=True): Withoutdrop=True, Pandas turns the old scattered row labels (16, 28, 41, ...) into an extra column named"index".
17.3.4 Vectorized Columns, .assign(), & Pandas 3.x Copy-on-Write
Derive clinical features cleanly with .assign(...) (or out = df.copy(); out["col"] = ...) so your raw dataset remains 100% immutable:
enriched = df.assign(
bmi=(df["weight_kg"] / (df["height_cm"] / 100) ** 2).round(2),
is_anemic=df["hemoglobin_g_dl"] < 11.0,
)
print("'bmi' in original df?", "bmi" in df.columns)
print("'bmi' in enriched? ", "bmi" in enriched.columns)
print("Anemic patients: ", int(enriched["is_anemic"].sum()))
cols = ["patient_id", "bmi", "hemoglobin_g_dl", "is_anemic"]
print(enriched[cols].head(3))Expected Output:
Notice that 'bmi' in original df? is False: .assign() always returns a new DataFrame with the added columns while leaving df pristine for downstream sensitivity checks.
17.3.5 Deterministic Multi-Key Sorting with .sort_values()
Whenever two patients share the exact same fever temperature, sorting by a single column leaves tie order arbitrary. Always pass a secondary tie-breaker column (patient_id) with an explicit ascending list:
Expected Output:
Look at rows 3 and 4: both patients recorded 38.7 °C, and ascending=[False, True] deterministically places UHC-0057 ahead of UHC-0108.
17.3.6 Reshaping Data: pd.melt, pd.crosstab, & pivot_table
Research pipelines constantly switch between wide visit tables (one column per timepoint), tidy long tables (one observation per row), and 2D contingency/summary matrices:
wide_bp = pd.DataFrame(
{
"patient_id": ["UHC-0001", "UHC-0002", "UHC-0003"],
"sbp_day1": [123, 132, 140],
"sbp_day2": [119, 128, 134],
}
)
long_bp = pd.melt(
wide_bp,
id_vars=["patient_id"],
value_vars=["sbp_day1", "sbp_day2"],
var_name="visit",
value_name="sbp",
)
print("Long BP shape:", long_bp.shape)
print(long_bp)
ct = pd.crosstab(df["district"], df["triage_priority"])
print("\nDistrict x Triage Priority counts:")
print(ct)
mean_temp = df.pivot_table(
index="district",
columns="triage_priority",
values="temp_c",
aggfunc="mean",
).round(2)
print("\nMean Temp (C) by District x Priority:")
print(mean_temp)Expected Output:
Long BP shape: (6, 3)
patient_id visit sbp
0 UHC-0001 sbp_day1 123
1 UHC-0002 sbp_day1 132
2 UHC-0003 sbp_day1 140
3 UHC-0001 sbp_day2 119
4 UHC-0002 sbp_day2 128
5 UHC-0003 sbp_day2 134
District x Triage Priority counts:
triage_priority Moderate Routine
district
Chattogram 7 11
Dhaka 6 10
Khulna 8 11
Rajshahi 12 12
Rangpur 15 14
Sylhet 8 6
Mean Temp (C) by District x Priority:
triage_priority Moderate Routine
district
Chattogram 37.50 37.15
Dhaka 37.68 37.31
Khulna 37.90 36.97
Rajshahi 37.78 37.00
Rangpur 37.89 36.87
Sylhet 38.05 36.82Notice how each reshaping tool plays a distinct role:
| Operation | Direction / Output | Best Research Use Case |
|---|---|---|
pd.melt(...) |
Wide \(\rightarrow\) Tidy Long | Unpivoting longitudinal visits (day1, day2) for plotting/regression |
pd.crosstab(r, c) |
Long \(\rightarrow\) Count Matrix | Integer contingency tables (automatically fills empty cells with 0) |
df.pivot_table(...) |
Long \(\rightarrow\) Aggregated Matrix | Group means/medians across two categorical axes (fill_value=0 optional) |
17.3.7 Break It On Purpose! (Pandas Error Anatomy)
Python’s Traceback:
Diagnosis: Column names in Pandas are exact dictionary keys—our dataset stores "hemoglobin_g_dl", not "hemoglobin".
Fix: Inspect list(df.columns) whenever you encounter a KeyError, and verify exact spelling and units (df["hemoglobin_g_dl"].mean()).
Python’s Traceback:
Diagnosis: Python’s logical and tries to collapse a 120-element boolean Series into a single scalar True/False, which is meaningless.
Fix: Replace and / or / not with elementwise bitwise & / | / ~, keeping parentheses around each comparison: (df["age_years"] >= 18) & (df["temp_c"] >= 38.0).
Python’s Traceback:
Diagnosis: A 120-row DataFrame has valid 0-based positions 0 through 119. Requesting .iloc[120] steps one slot past the last row!
Fix: Use df.iloc[-1] for the final row, or check len(df) before indexing by integer position.
17.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Scenario: A district health director wants a reproducible Adult Fever-Rate Audit from clinic_triage: restrict to adult patients (age_years >= 18), compute each adult’s 2-decimal BMI and has_fever flag (temp_c >= 38.0) without mutating df, tabulate afebrile vs. febrile counts per district, calculate fever_pct rounded to 1 decimal place, and rank districts from highest to lowest fever burden.
17.4.1 Blueprint (Completed on Paper Before Coding)
Purity & Cohort Selection Contract:
- Filter adults with
df.loc[df["age_years"] >= 18].copy()so the original 120-row table remains untouched (len(df.columns) == 11). - Add
bmiandhas_fevervia.assign(bmi=lambda d: ..., has_fever=lambda d: ...)and reset the index (drop=True).
- Filter adults with
Contingency & Rate Formula:
- Build a 2-column count table via
pd.crosstab(cohort["district"], cohort["has_fever"])(False\(\rightarrow\)afebrile,True\(\rightarrow\)febrile). - Compute \(\text{fever\_pct} = \text{round}\!\left(\frac{\text{febrile}}{\text{afebrile} + \text{febrile}} \times 100,\ 1\right)\).
- Build a 2-column count table via
Ranking & Hand Check:
- Sort descending by
fever_pctusing.sort_values("fever_pct", ascending=False). - Verify row sums match the 97-adult cohort (
27 + 16 + 9 + 20 + 12 + 13 = 97) and originaldfstill has 11 columns.
- Sort descending by
17.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
cohort = (
df.loc[df["age_years"] >= 18]
.copy()
.assign(
bmi=lambda d: (
d["weight_kg"] / (d["height_cm"] / 100) ** 2
).round(2),
has_fever=lambda d: d["temp_c"] >= 38.0,
)
.reset_index(drop=True)
)
fever_table = pd.crosstab(cohort["district"], cohort["has_fever"])
fever_table.columns = ["afebrile", "febrile"]
total_adults = fever_table["afebrile"] + fever_table["febrile"]
fever_table["fever_pct"] = (
fever_table["febrile"] / total_adults * 100
).round(1)
ranked = fever_table.sort_values("fever_pct", ascending=False)
print(
"Adult cohort size:", len(cohort),
"| Original df cols:", len(df.columns),
)
print(ranked)Output:
17.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_04_data_science_math/ch17_pandas_foundations/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and implement all six mutation-free Pandas functions below. Pass each function name (without parentheses!) to tutor.check(...)—for example, tutor.check("ch17_todo_01", add_bmi_column).
- To-Do 17.1 (Vectorized Feature —
add_bmi_column(df)): Return a fresh copy ofdfwith a new'bmi'column computed as(weight_kg / (height_cm / 100) ** 2).round(2), without mutatingdf. - To-Do 17.2 (Compound Boolean Filtering —
filter_urgent_adults(df)): Filter rows whereage_years >= 18andtriage_priority == "Urgent", reset index withdrop=True, and return a copy. - To-Do 17.3 (Multi-Key Sorting —
select_top_fever_patients(df, n=5)): Sort bytemp_cdescending thenpatient_idascending, return topnrows with exact columns["patient_id", "district", "temp_c"], and reset index (drop=True). - To-Do 17.4 (Summary Statistics Dict —
summarize_numeric_vitals(df, cols)): Return adictmapping eachcolincolsto{"mean": float, "median": float, "std": float}rounded to 2 decimals using Pandas’ defaultddof=1. - To-Do 17.5 (Wide-to-Long Unpivoting —
melt_vital_measurements(wide_df)): Usepd.meltto reshape["patient_id", "sbp_day1", "sbp_day2"]into tidy long columns["patient_id", "visit", "sbp"]. - To-Do 17.6 (Contingency Table —
pivot_district_priority_counts(df)): Return a contingency DataFrame withdistrictas index,triage_priorityas columns, and integer counts filled with0.
17.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Trace every Pandas expression carefully on paper before checking Stage 8:
- Single vs. Double Brackets: Given a 120-row DataFrame
df, what are the Python types and.shapeattributes ofdf["temp_c"]versusdf[["temp_c"]]? .locvs..ilocEndpoint Rule: For a DataFrame with default integer index0, 1, 2, 3, 4, how many rows are returned bydf.iloc[1:3]versusdf.loc[1:3]?- Series Boolean Operators: Why does
(df["age_years"] >= 18) and (df["temp_c"] >= 38.0)raiseValueError, and what two edits fix it? - Operator Precedence Bug: Why does
df["age_years"] >= 18 & df["temp_c"] >= 38.0(without parentheses) fail even though&is used? - The
inplace=TrueReturn Trap: What value is stored insorted_dfafter runningsorted_df = df.sort_values("temp_c", inplace=True)? - Leftover
'index'Column: You filter a DataFrame to 4 matching rows (original indices16, 28, 41, 77) and call.reset_index()withoutdrop=True. What unexpected column gets added? - Pandas vs. NumPy Standard Deviation: For the Series
s = pd.Series([10.0, 12.0, 14.0]), mean \(\bar{x} = 12.0\) and sum of squared deviations is \(8.0\). What doess.std()return (ddof=1) versuss.std(ddof=0)? - Wide-to-Long Row Count: If
wide_dfhas 45 patients and 3 follow-up columns (sbp_day1,sbp_day2,sbp_day3) pluspatient_id, what is the exact.shapeofpd.melt(wide_df, id_vars=["patient_id"], value_vars=["sbp_day1", "sbp_day2", "sbp_day3"])? - Zero-Count District Pitfall: If Khulna has 19 total patients and
0Urgent patients, what happens to Khulna if you filterdf[df["triage_priority"] == "Urgent"]["district"].value_counts()instead of usingpd.crosstab? - Non-Mutating Pipeline Safety: Why should a helper function
add_bmi_column(df)start without = df.copy()ordf.assign(...)rather than executingdf["bmi"] = ...directly on the parameterdf?
17.7 Stage 7 — Chapter 17 Hands-On Coding Exam (100 Points)
Open part_04_data_science_math/ch17_pandas_foundations/03_coding_exam.ipynb, implement all three functions, and run tutor.check_exam("ch17", exam_submissions) to earn your score out of 100 points.
17.7.1 Exam Task 17.1 (30 Points): prepare_triage_cohort(df)
Implement prepare_triage_cohort(df):
- Work on a non-mutating copy so the caller’s
dfis completely unchanged. - Add
'bmi'computed as(weight_kg / (height_cm / 100) ** 2).round(2). - Add boolean column
'is_anemic'(hemoglobin_g_dl < 11.0, strict<). - Keep only adult rows (
age_years >= 18) and reset the index withdrop=True.
17.7.3 Exam Task 17.3 (35 Points): reshape_and_compare_visits(wide_df)
Implement reshape_and_compare_visits(wide_df):
- Given
wide_dfwith columns["patient_id", "hb_baseline", "hb_followup"], melt it into a long DataFrame with exact columns["patient_id", "visit", "hb_g_dl"]. - Compute the mean paired change (follow-up minus baseline) rounded to 2 decimal places as a native Python
float:float(round((wide_df["hb_followup"] - wide_df["hb_baseline"]).mean(), 2)). - Return the 2-tuple
(long_df, mean_change)without mutatingwide_df.
17.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
17.8.1 A. Concept & Execution Trace Test Key
df["temp_c"]is a 1DSerieswith shape(120,);df[["temp_c"]]is a 2DDataFramewith shape(120, 1).df.iloc[1:3]returns 2 rows (positions1, 2— exclusive stop);df.loc[1:3]returns 3 rows (labels1, 2, 3— inclusive endpoints).- Python’s
andcannot evaluate a multi-element booleanSeriesas a single truth value. Replaceandwith&and keep parentheses around each condition. - Bitwise
&binds tighter than>=, so Python tries to evaluate18 & df["temp_c"]first, raisingTypeError. Always parenthesize both sides. None— Pandas methods invoked withinplace=TruereturnNoneinstead of the sorted DataFrame.- An extra column named
"index"containing[16, 28, 41, 77]is inserted as the first column. Always passdrop=Trueunless you want to preserve old labels as data. s.std()(ddof=1) returns \(\sqrt{8.0 / 2} = \mathbf{2.0}\);s.std(ddof=0)returns \(\sqrt{8.0 / 3} \approx \mathbf{1.633}\).(135, 3)— \(45 \times 3 = 135\) rows and 3 columns (patient_id,variable,value).- Khulna is silently dropped from the output instead of appearing with
0Urgent patients (0.0%).pd.crosstabkeeps every district and fills missing cells with integer0. - Assigning
df["bmi"] = ...directly mutates the caller’s DataFrame as a hidden side effect, breaking reproducibility if the caller expects the original schema later in the notebook.
17.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
import pandas as pd
# To-Do 17.1
def add_bmi_column(df):
"""Return a fresh copy of df with 2-decimal BMI column added."""
out = df.copy()
height_m = out["height_cm"] / 100
out["bmi"] = (out["weight_kg"] / (height_m ** 2)).round(2)
return out
# To-Do 17.2
def filter_urgent_adults(df):
"""Filter adult Urgent rows, reset index, and return a copy."""
mask = (df["age_years"] >= 18) & (df["triage_priority"] == "Urgent")
return df.loc[mask].reset_index(drop=True).copy()
# To-Do 17.3
def select_top_fever_patients(df, n=5):
"""Return top-n fever patients sorted by temp_c desc, id asc."""
cols = ["patient_id", "district", "temp_c"]
ranked = df.sort_values(
by=["temp_c", "patient_id"],
ascending=[False, True],
)
return ranked[cols].head(n).reset_index(drop=True)
# To-Do 17.4
def summarize_numeric_vitals(df, cols):
"""Return {col: {'mean', 'median', 'std'}} rounded to 2 decimals."""
summary = {}
for col in cols:
s = df[col]
summary[col] = {
"mean": float(round(s.mean(), 2)),
"median": float(round(s.median(), 2)),
"std": float(round(s.std(), 2)),
}
return summary
# To-Do 17.5
def melt_vital_measurements(wide_df):
"""Unpivot sbp_day1/sbp_day2 to long [patient_id, visit, sbp]."""
return pd.melt(
wide_df,
id_vars=["patient_id"],
value_vars=["sbp_day1", "sbp_day2"],
var_name="visit",
value_name="sbp",
)
# To-Do 17.6
def pivot_district_priority_counts(df):
"""Return integer district x triage_priority counts filled with 0."""
return pd.crosstab(df["district"], df["triage_priority"])17.8.3 C. Annotated Solutions — Chapter 17 Coding Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
# Exam Task 17.1 (30 pts)
def prepare_triage_cohort(df):
"""Return non-mutating adult triage cohort with bmi and is_anemic."""
adults = df.loc[df["age_years"] >= 18].copy()
height_m = adults["height_cm"] / 100
adults["bmi"] = (adults["weight_kg"] / (height_m ** 2)).round(2)
adults["is_anemic"] = adults["hemoglobin_g_dl"] < 11.0
return adults.reset_index(drop=True)
# Exam Task 17.2 (35 pts)
def rank_districts_by_urgent_share(df):
"""Rank districts by % Urgent patients desc, then district asc."""
is_urgent = df["triage_priority"] == "Urgent"
counts = pd.crosstab(df["district"], is_urgent)
total = counts.sum(axis=1)
urgent = (
counts[True]
if True in counts.columns
else pd.Series(0, index=total.index)
)
out = pd.DataFrame(
{
"district": total.index,
"total_patients": total.astype(int).values,
"urgent_patients": urgent.astype(int).values,
}
)
out["urgent_pct"] = (
out["urgent_patients"] / out["total_patients"] * 100
).round(1)
return out.sort_values(
by=["urgent_pct", "district"],
ascending=[False, True],
).reset_index(drop=True)
# Exam Task 17.3 (35 pts)
def reshape_and_compare_visits(wide_df):
"""Melt baseline/followup Hb and return (long_df, mean_change)."""
long_df = pd.melt(
wide_df,
id_vars=["patient_id"],
value_vars=["hb_baseline", "hb_followup"],
var_name="visit",
value_name="hb_g_dl",
)
deltas = wide_df["hb_followup"] - wide_df["hb_baseline"]
mean_change = float(round(deltas.mean(), 2))
return long_df, mean_change17.8.4 D. Debugging Post-Mortem: Top 3 Chapter 17 Failure Modes
- Mutating Input DataFrames or Using
inplace=True: Writingdf["bmi"] = ...directly on an argument leaks columns back into the caller’s table, anddf = df.sort_values(..., inplace=True)setsdftoNone. Always copy (out = df.copy()or.assign(...)) and return the fresh DataFrame. - Mixing Python
and/or/notwith Pandas Series Masks: Every Pandas boolean mask is a vector of booleans, not a single truth value. Always write(cond_a) & (cond_b),(cond_a) | (cond_b), or~(cond_a)with parentheses around each condition. - Dropping Zero-Count Categories During Filtering: Filtering to
"Urgent"rows before counting districts erases zero-urgent districts from denominator tables and prevalence rankings. Usepd.crosstabor full-table boolean sums so every district stays accounted for.
You can now index, filter, sort, and reshape clean clinical tables with Pandas 3.x Copy-on-Write discipline. Real-world field datasets, however, arrive with missing NaN values, duplicate IDs, messy text labels, and multiple tables that must be joined together. In Chapter 18, you will master missing-data diagnostics and imputation, split-apply-combine .groupby() pipelines, and relational .merge() joins across multi-table clinical cohorts.


