0Pricing
Data Science Academy · 课时

使用均值、中位数或众数填补

按列类型选择合理的填充值

使用均值、中位数或众数填补 是 CoddyKit 上的免费 Data Science Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Data Science Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Data Science Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「使用均值、中位数或众数填补」课时是免费的吗?

是的 — 「使用均值、中位数或众数填补」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Data Science Academy 课程的其余内容,请升级到 CoddyKit PRO。 Data Science Academy 课程共包含 4 节课。

「使用均值、中位数或众数填补」这节课中我会学到什么?

按列类型选择合理的填充值 你通过在浏览器中直接运行的动手代码来练习 Data Science Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Data Science Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Data Science Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「使用均值、中位数或众数填补」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Data Science Academy 课中编写并运行代码吗?

能。每节 Data Science Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 找出表格中隐藏的 NaNs
  2. 删除还是填充:明智选择
  3. 使用均值、中位数或众数填补
  4. 修正 dtypes 并删除重复行
← 返回 Data Science Academy