LeetCode 题解工作台
删去丢失的数据
DataFrame students +-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +----…
0
题型
1
代码语言
0
相关题
当前训练重点
简单 · Drop Missing Data core interview pattern
答案摘要
import pandas as pd def dropMissingData(students: pd.DataFrame) -> pd.DataFrame:
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 Drop Missing Data core interview pattern 题型思路
题目描述
DataFrame students +-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +-------------+--------+
在 name 列里有一些具有缺失值的行。
编写一个解决方案,删除具有缺失值的行。
返回结果格式如下示例所示。
示例 1:
输入: +------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 32 | Piper | 5 | | 217 | None | 19 | | 779 | Georgia | 20 | | 849 | Willow | 14 | +------------+---------+-----+ 输出: +------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 32 | Piper | 5 | | 779 | Georgia | 20 | | 849 | Willow | 14 | +------------+---------+-----+ 解释: 学号为 217 的学生所在行在 name 列中有空值,因此这一行将被删除。
解题思路
方法一
import pandas as pd
def dropMissingData(students: pd.DataFrame) -> pd.DataFrame:
return students[students['name'].notnull()]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidate chooses an appropriate built-in function for the task.
- question_mark
Candidate demonstrates an understanding of handling missing data efficiently in pandas.
- question_mark
Candidate avoids unnecessary in-place operations, favoring clean code practices.
常见陷阱
外企场景- error
Not specifying the correct column in `dropna()` can lead to dropping unnecessary rows.
- error
Forgetting to return the new DataFrame when `inplace=True` is avoided.
- error
Misunderstanding the use of `subset` in `dropna()`, leading to incorrect results.
进阶变体
外企场景- arrow_right_alt
Instead of `dropna()`, use filtering methods like `isnull()` combined with `notnull()` for more granular control over missing data.
- arrow_right_alt
Consider filling missing values with a default value using `fillna()` if deletion is not desirable.
- arrow_right_alt
Instead of using pandas, solve the problem using a different library such as NumPy or Python's built-in data structures.