You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python删除特定变量名及数据集中‘Not Reported’行的方法咨询

问题1:如何在Python中删除特定变量名?
  • 使用del语句直接删除变量:
    x = 10
    del x  # 删除变量x
    
    执行后再访问x会抛出NameError,说明变量已被成功删除。
  • 若需在全局/局部命名空间中安全删除(避免变量不存在时报错),可操作命名空间字典:
    globals().pop('x', None)  # 安全删除全局变量x,变量不存在时无报错
    
问题2:删除数据集中包含"Not Reported"的行

df.dropna()仅处理缺失值(NaN),而你的数据里"Not Reported"是字符串类型,因此无法被识别。可通过以下两种方法解决:

方法1:替换为缺失值后删除

先将所有"Not Reported"替换为缺失值,再用dropna()删除对应行:

import pandas as pd

# 把目标字符串替换为缺失值
df.replace("Not Reported", pd.NA, inplace=True)
# 删除包含缺失值的行
df.dropna(inplace=True)

方法2:直接筛选排除目标行

直接过滤掉任意列包含"Not Reported"的行:

# 保留所有不包含"Not Reported"的行
df = df[~df.isin(["Not Reported"]).any(axis=1)]

其中isin(["Not Reported"])检查每个元素是否匹配目标字符串,any(axis=1)标记该行是否存在匹配项,~取反后得到需要保留的行。

内容的提问来源于stack exchange,提问作者nur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 00:37:26