LeetCode 题解工作台

删去丢失的数据

DataFrame students +-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +----…

category

0

题型

code_blocks

1

代码语言

hub

0

相关题

当前训练重点

简单 · Drop Missing Data core interview pattern

bolt

答案摘要

import pandas as pd def dropMissingData(students: pd.DataFrame) -> pd.DataFrame:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 Drop Missing Data core interview pattern 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

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 列中有空值,因此这一行将被删除。
lightbulb

解题思路

方法一

1
2
3
4
5
6
import pandas as pd


def dropMissingData(students: pd.DataFrame) -> pd.DataFrame:
    return students[students['name'].notnull()]
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

删去丢失的数据题解:Drop Missing Data core … | LeetCode #2883 简单