Impute With Mean, Median, or Mode
Sensible fills per column type.
Impute With Mean, Median, or Mode is a free Data Science Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Filling With a Statistic
Instead of inventing random numbers, you fill gaps with a summary value from the column itself. This careful approach is called imputation. 🧮
The Mean Fill
For numbers, the simplest choice is the mean, the column average. It keeps the overall total roughly intact when gaps are few.
df['age'].fillna(df['age'].mean())When the Mean Misleads
The mean is fragile: a few huge values drag it far from the center. On skewed data, mean imputation can pull every gap toward an unrealistic number.
The Median Fill
The median is the middle value, and it shrugs off extreme outliers. For skewed numeric columns it is usually the safer fill.
df['income'].fillna(df['income'].median())Mean or Median?
A quick rule: reach for the median when a column has outliers or a long tail, and the mean only when values are fairly symmetric.
The Mode for Categories
Text and category columns have no average. For them you fill with the mode, the most frequent value, since that is the most likely fit.
top = df['city'].mode()[0]
df['city'].fillna(top)Why mode Returns a Series
A column can tie for most common, so mode() returns a Series of all winners. Pick the first with index 0 when you need one value.
df['city'].mode()[0]Fill Per Column Type
The best practice is to impute each column by its type: median for skewed numbers, mean for symmetric ones, and mode for categories.
Forward and Back Fill
For ordered data like time series, carry the last known value forward with ffill, or pull the next one backward with bfill.
df['temp'].ffill()The Hidden Cost
Every imputation shrinks the column's natural spread, because filled values cluster at one point. Note this, since it nudges your variance downward.
Flag What You Filled
A pro habit: add a boolean column marking which rows were imputed. That flag lets later analysis know which values were real and which were guessed.
df['age_filled'] = df['age'].isna()Quick Check
An income column is heavily skewed by a few millionaires. Which imputation fits best?
Recap: Smart Fills
You now impute with mean, median, or mode by column type, use ffill for ordered data, and flag filled rows so nothing gets hidden.
Frequently asked questions
Is the “Impute With Mean, Median, or Mode” lesson free?
Yes — the full text of “Impute With Mean, Median, or Mode” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Impute With Mean, Median, or Mode”?
Sensible fills per column type. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Impute With Mean, Median, or Mode” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Find the NaNs Hiding in Your Table
- Drop vs Fill: Choosing Wisely
- Impute With Mean, Median, or Mode
- Fix dtypes and Duplicate Rows