fix(第二章第一节): 修正缺失值检索示例的错误写法与思考题答案 - #26
Open
ksk2023 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
《第二章:第一节数据清洗及特征处理.ipynb》(答案版)2.1.2 节的缺失值检索示例存在三处错误,感谢 @BigBiggerBest 在 #15 中的指正:
df[df['Age']==None]=0:== None无法匹配read_csv读入后 float64 列中的NaN,检索结果恒为空,示例起不到演示作用;df[df['Age'].isnull()] = 0:布尔索引赋值会把命中行整行所有列都置 0(不是只改 Age),直接污染了df。本 notebook 是顺序执行的,后续 2.2 节df[df.duplicated()]输出里全是 0 的"重复行"、drop_duplicates、to_csv('test_clear.csv')等全部基于这份被污染的数据,保存下来的test_clear.csv等产物也都是错的;df[df['Age'] == np.nan] = 0:NaN遵循 IEEE 754 不等于任何值,== np.nan恒为False,同样检索不到;== np.nan同样匹配不到NaN。修复
= 0整行赋值),并在注释中说明各自检索不到/会破坏数据的原因;只填充某一列应使用df['Age'].fillna(0);None与np.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 重复行"正是数据被污染的产物);验证说明
Fixes #15