删除还是填充:明智选择
何时删除,何时填补
删除还是填充:明智选择 是 CoddyKit 上的免费 Data Science Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Data Science Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Data Science Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Two Roads at a Gap
When you meet missing data, you face one big choice: drop the affected rows, or fill them in. The right road depends on how much you lose either way. 🛣️
Dropping Rows
The method dropna() removes any row that has at least one missing value. It is clean and simple, but it can quietly delete a lot of data.
clean = df.dropna()Dropping Columns Instead
If one column is mostly empty, drop the whole column with axis=1. Sometimes a single bad column hurts more than the rows it sits in.
df.dropna(axis=1)When Dropping Is Safe
Dropping works well when gaps are rare and scattered. Losing a tiny fraction of a large dataset rarely changes your conclusions.
When Dropping Hurts
If many rows share even one gap, dropping can shrink your data drastically. Worse, the rows you lose may not be random, which biases results.
Filling the Gaps
The alternative is to fill missing cells with a sensible value using fillna(). You keep every row, at the cost of inventing some numbers.
df.fillna(0)Filling Is a Guess
Remember that every filled value is an estimate. A good fill is reasonable and documented; a careless one hides the gap and warps your stats.
The thresh Middle Ground
You can keep rows that have enough real data using thresh, which sets the minimum number of non-missing values a row must have to survive.
df.dropna(thresh=3)Target One Column
Use subset to drop rows only when a specific key column is missing. This protects the rest of your data from unnecessary deletion.
df.dropna(subset=['price'])Ask Why It Is Missing
Before deciding, ask why the value is absent. Data missing at random is safe to fill, but a systematic gap may itself be a meaningful signal.
A Simple Rule of Thumb
A handy guide: drop when gaps are few, fill when rows are precious. Always weigh how each choice reshapes your distribution before committing.
Quick Check
A column is 80% empty but every row has useful data elsewhere. What is usually the wisest move?
Recap: Choose With Intent
You now weigh drop versus fill by how much data you lose and why values are missing. Use dropna, thresh, and subset to delete with care.
常见问题解答
「删除还是填充:明智选择」课时是免费的吗?
是的 — 「删除还是填充:明智选择」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Data Science Academy 课程的其余内容,请升级到 CoddyKit PRO。 Data Science Academy 课程共包含 4 节课。
「删除还是填充:明智选择」这节课中我会学到什么?
何时删除,何时填补 你通过在浏览器中直接运行的动手代码来练习 Data Science Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Data Science Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Data Science Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「删除还是填充:明智选择」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Data Science Academy 课中编写并运行代码吗?
能。每节 Data Science Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 找出表格中隐藏的 NaNs
- 删除还是填充:明智选择
- 使用均值、中位数或众数填补
- 修正 dtypes 并删除重复行