From 05946a3ee6c042b499af7ba91d82b8b6d0f2b7db Mon Sep 17 00:00:00 2001 From: FAQ Bot Date: Thu, 1 Oct 2026 09:10:32 +0000 Subject: [PATCH] NEW: Homework 1 Q6: Why does the median of horsepower change after filling NA --- ...6-median-changes-after-filling-nas-with-mode.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 _questions/machine-learning-zoomcamp/module-1-homework/009_66e86fef32_hw1-q6-median-changes-after-filling-nas-with-mode.md diff --git a/_questions/machine-learning-zoomcamp/module-1-homework/009_66e86fef32_hw1-q6-median-changes-after-filling-nas-with-mode.md b/_questions/machine-learning-zoomcamp/module-1-homework/009_66e86fef32_hw1-q6-median-changes-after-filling-nas-with-mode.md new file mode 100644 index 00000000..edf6f35e --- /dev/null +++ b/_questions/machine-learning-zoomcamp/module-1-homework/009_66e86fef32_hw1-q6-median-changes-after-filling-nas-with-mode.md @@ -0,0 +1,14 @@ +--- +id: 66e86fef32 +question: 'Homework 1 Q6: Why does the median of horsepower change after filling NAs + with the most frequent value?' +sort_order: 9 +--- + +In pandas, filling missing values with the column mode can change the median because the median is sensitive to how many values fall below vs. above it. + +In this case, `df.horsepower.mode()[0]` (the most frequent value) is `252.0`, while the original median is `254.0`. When you replace all `877` missing values with `252.0`, you add a large block of values below the original median, so the median shifts down to `252.0`. + +A couple of implementation notes: +- `mode()` returns a Series (to handle ties). That’s why you use `[0]` to get the single most common value: `df.horsepower.mode()[0]`. +- `fillna()` does not modify the DataFrame in place unless you assign the result back (e.g., `df['horsepower'] = df['horsepower'].fillna(mode)`, or use the returned Series/column in your next calculation). \ No newline at end of file