Skip to content

fix(第二章第一节): 修正缺失值检索示例的错误写法与思考题答案 - #26

Open
ksk2023 wants to merge 1 commit into
datawhalechina:masterfrom
ksk2023:fix/ch2-sec1-nan-answer
Open

fix(第二章第一节): 修正缺失值检索示例的错误写法与思考题答案#26
ksk2023 wants to merge 1 commit into
datawhalechina:masterfrom
ksk2023:fix/ch2-sec1-nan-answer

Conversation

@ksk2023

@ksk2023 ksk2023 commented Aug 19, 2026

Copy link
Copy Markdown

问题

《第二章:第一节数据清洗及特征处理.ipynb》(答案版)2.1.2 节的缺失值检索示例存在三处错误,感谢 @BigBiggerBest 在 #15 中的指正:

  1. cell df[df['Age']==None]=0== None 无法匹配 read_csv 读入后 float64 列中的 NaN,检索结果恒为空,示例起不到演示作用;
  2. cell df[df['Age'].isnull()] = 0:布尔索引赋值会把命中行整行所有列都置 0(不是只改 Age),直接污染了 df。本 notebook 是顺序执行的,后续 2.2 节 df[df.duplicated()] 输出里全是 0 的"重复行"、drop_duplicatesto_csv('test_clear.csv') 等全部基于这份被污染的数据,保存下来的 test_clear.csv 等产物也都是错的;
  3. cell df[df['Age'] == np.nan] = 0NaN 遵循 IEEE 754 不等于任何值,== np.nan 恒为 False,同样检索不到;
  4. 思考题【回答】("数值列读取数据后,空缺值的数据类型为 float64 所以用 None 一般索引不到,比较的时候最好用 np.nan"):结论是错的——== np.nan 同样匹配不到 NaN

修复

  • 三个示例 cell 改为纯检索(去掉破坏性的 = 0 整行赋值),并在注释中说明各自检索不到/会破坏数据的原因;只填充某一列应使用 df['Age'].fillna(0)
  • 修正思考题【回答】:Nonenp.nan 都不能用 == 检索,推荐 .isnull() / .isna()(ndarray 用 np.isnan());
  • 用仓库自带 train.csv 对整个 notebook(55 个 cell)重新顺序执行,刷新了所有被污染的输出:
    • isnull().sum() 输出 Age=177 / Cabin=687 / Embarked=2,与数据集事实一致;
    • df[df.duplicated()] 现在正确输出 Empty DataFrame(原来的"全 0 重复行"正是数据被污染的产物);
    • 重新执行 0 错误。

验证说明

  • 仅改动上述 4 个 cell 的内容与其连带刷新的 outputs,其余 cell 的代码未改动;
  • 《…-课程.ipynb》(学生练习版)对应 cell 为空白填空,无此问题,未改动。

Fixes #15

Copilot AI lite review requested due to automatic review settings August 19, 2026 11:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

本次 PR 修正《第二章:第一节数据清洗及特征处理.ipynb》(答案版)中缺失值检索示例的错误写法,避免示例代码对 df 产生破坏性污染,并同步更正相关思考题答案与执行输出,使 notebook 顺序执行后的结果与数据集事实一致。

Changes:

  • 将 2.1.2 节 3 个缺失值检索示例改为“纯检索”展示,并补充解释为何原写法会失效/污染数据
  • 修正思考题【回答】,明确 None/np.nan 都不能用 == 检索缺失值,推荐 .isnull()/.isna()(ndarray 用 np.isnan()
  • 重新顺序执行 notebook 并更新受影响的 outputs(如 duplicated / isnull().sum() 等)
Suppressed comments (3)

第二章项目集合/第二章:第一节数据清洗及特征处理.ipynb:31

  • This notebook now includes per-cell execution timestamp metadata (e.g., iopub.execute_input/status.*). Other notebooks in this repo keep cell metadata empty (e.g., 第二章项目集合/第二章:第二节数据重构1.ipynb:22-25 has "metadata": {}). Consider stripping the "execution" metadata blocks to reduce diff noise and keep notebooks consistent.
    第二章项目集合/第二章:第一节数据清洗及特征处理.ipynb:561
  • The note says to use df['Age'].fillna(0) to fill only the Age column, but that expression alone does not modify df unless you assign it back (or use an explicit in-place operation). This could mislead readers into thinking the column is actually filled.
    第二章项目集合/第二章:第一节数据清洗及特征处理.ipynb:2956
  • The regex string literal contains a backslash escape (.) which can raise SyntaxWarning on newer Python versions (invalid escape sequence) when not written as a raw string. Prefer a raw string literal for regex patterns here.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

第二章项目集合-第二章:第一节数据清洗及特征处理.ipynb部分答案错误

2 participants