Python Pandas从一列字符串的数据选择中过滤掉Nan [英] Python pandas Filtering out nan from a data selection of a column of strings

查看:1868
本文介绍了Python Pandas从一列字符串的数据选择中过滤掉Nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不使用groupby的情况下如何在没有NaN的情况下过滤出数据?

Without using groupby how would I filter out data without NaN?

假设我有一个矩阵,客户可以在其中填写"N/A","n/a"或其任何变体,而其他人则将其留空:

Let say I have a matrix where customers will fill in 'N/A','n/a' or any of its variations and others leave it blank:

import pandas as pd
import numpy as np


df = pd.DataFrame({'movie': ['thg', 'thg', 'mol', 'mol', 'lob', 'lob'],
                  'rating': [3., 4., 5., np.nan, np.nan, np.nan],
                  'name': ['John', np.nan, 'N/A', 'Graham', np.nan, np.nan]})

nbs = df['name'].str.extract('^(N/A|NA|na|n/a)')
nms=df[(df['name'] != nbs) ]

输出:

>>> nms
  movie    name  rating
0   thg    John       3
1   thg     NaN       4
3   mol  Graham     NaN
4   lob     NaN     NaN
5   lob     NaN     NaN

我将如何过滤NaN值,以便可以像这样使用结果:

How would I filter out NaN values so I can get results to work with like this:

  movie    name  rating
0   thg    John       3
3   mol  Graham     NaN

我猜我需要类似~np.isnan的东西,但tilda不能与字符串一起使用.

I am guessing I need something like ~np.isnan but the tilda does not work with strings.

推荐答案

只需删除它们:

nms.dropna(thresh=2)

这将删除所有至少有两个非NaN的行.

this will drop all rows where there are at least two non-NaN.

然后您可以将其放置在名称为NaN的位置:

Then you could then drop where name is 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调用即可完成此操作:

Actually looking at what you originally want you can do just this without the dropna call:

nms[nms.name.notnull()]

更新

3年后,针对这个问题,出现了一个错误,首先是 thresh arg至少查找n个非NaN值,因此实际上输出应为:

Looking at this question 3 years later, there is a mistake, firstly thresh arg looks for at least n non-NaN values so in fact the output should be:

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年前弄错了,或者我运行的熊猫版本存在错误,这两种情况都是可能的.

It's possible that I was either mistaken 3 years ago or that the version of pandas I was running had a bug, both scenarios are entirely possible.

这篇关于Python Pandas从一列字符串的数据选择中过滤掉Nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆