您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python:如何删除特定列为空/ NaN的行?

Python:如何删除特定列为空/ NaN的行?

使用dropna带有参数subset的指定检查列NaNS:

data = data.dropna(subset=['sms'])
print (data)
   id city department   sms  category
1   2  lhr    revenue  good         1

用另一种解决方boolean indexingnotnull

data = data[data['sms'].notnull()]
print (data)
   id city department   sms  category
1   2  lhr    revenue  good         1

另一种选择query

print (data.query("sms == sms"))
   id city department   sms  category
1   2  lhr    revenue  good         1

#[300000 rows x 5 columns]
data = pd.concat([data]*100000).reset_index(drop=True)

In [123]: %timeit (data.dropna(subset=['sms']))
100 loops, best of 3: 19.5 ms per loop

In [124]: %timeit (data[data['sms'].notnull()])
100 loops, best of 3: 13.8 ms per loop

In [125]: %timeit (data.query("sms == sms"))
10 loops, best of 3: 23.6 ms per loop
python 2022/1/1 18:42:13 有284人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶