Data Wrangling with pandas
pandas for traceable biological data wrangling
A pandas DataFrame is a labeled table. Start by inspecting column names, data types, and a small sample of rows before transforming anything. Keep one row per observational unit and one column per variable; this makes filters, group summaries, and merges auditable.
Boolean filtering selects rows that meet a condition, usually with df.loc[condition, columns]. Grouping followed by an explicit aggregation such as groupby(...).agg(...) summarizes observations without discarding the code that defines the summary. Missing values deserve an explicit choice: inspect them, then drop, impute, or retain them according to a documented scientific reason.
A merge combines tables by a key. Before merging, check that a supposed sample identifier is unique in the table where one record is expected; otherwise a many-to-many merge can duplicate measurements.
After each transformation, check row count, key uniqueness, and a few known records. Code is reproducible only when its assumptions about rows, variables, and keys are visible.
Mark Yourself Question
- Grab a piece of paper and try this problem yourself.
- When you're done, check the "I have answered this question" box below.
- View the solution and report whether you got it right or wrong.
A DataFrame df contains numeric column mass_mg. Write one pandas assignment that creates mass_g by converting mg to g, and state the value when mass_mg is 2750.
A table has one row per culture and columns medium and growth. Which pandas expression computes mean growth for each medium?