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

Python Pandas从一列字符串的数据选择中过滤掉Nan

Python Pandas从一列字符串的数据选择中过滤掉Nan

放下它们:

nms.dropna(thresh=2)

这将删除所有至少有两个non-的行NaN

然后,您可以将名称放在哪里NaN

In [87]:

nms
Out[87]:
  movie    name  rating
0   thg    John       3
1   thg     NaN       4
3   mol  Graham     NaN
4   lob     NaN     NaN
5   lob     NaN     NaN

[5 rows x 3 columns]
In [89]:

nms = nms.dropna(thresh=2)
In [90]:

nms[nms.name.notnull()]
Out[90]:
  movie    name  rating
0   thg    John       3
3   mol  Graham     NaN

[2 rows x 3 columns]

实际查看您最初想要的是什么,而无需dropna调用即可:

nms[nms.name.notnull()]

3年后的这个问题,有一个错误,首先thresharg至少查找nNaN值,因此实际上输出应为:

In [4]:
nms.dropna(thresh=2)

Out[4]:
  movie    name  rating
0   thg    John     3.0
1   thg     NaN     4.0
3   mol  Graham     NaN

我可能是3年前弄错了,或者我运行的熊猫版本存在错误,这两种情况都是可能的。

python 2022/1/1 18:46:23 有331人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

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

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

请先登录

推荐问题


联系我
置顶