Python 选择具有 None 值的熊猫单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26893419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:07:19  来源:igfitidea点击:

Selecting pandas cells with None value

pythonpandasnullna

提问by Michael Discenza

I have a column of a pandas dataframe that I got from a database query with blank cells. The blank cells become "None" and I want to check if each of the rows is None:

我有一列熊猫数据框,是从带有空白单元格的数据库查询中获得的。空白单元格变为“无”,我想检查每一行是否为无:

In [325]: yes_records_sample['name']
Out[325]: 
41055    John J Murphy Professional Building
25260                                   None
41757             Armand Bayou Nature Center
31397                                   None
33104               Hubert Humphrey Building
16891                         Williams Hall
29618                                   None
3770                          Covenant House
39618                                   None
1342       Bhathal Student Services Building
20506                                   None

My understanding per the documentation is that I can check if each row is null with isnull()command http://pandas.pydata.org/pandas-docs/dev/missing_data.html#values-considered-missing

我对文档的理解是,我可以使用isnull()命令http://pandas.pydata.org/pandas-docs/dev/missing_data.html#values-thinked-missing检查每一行是否为空

That function, however, is not working for me:

但是,该功能对我不起作用:

In [332]: isnull(yes_records_sample['name'])

I get the following error:

我收到以下错误:

NameError Traceback (most recent call last)
<ipython-input-332-55873906e7e6> in <module>()
----> 1 isnull(yes_records_sample['name'])
NameError: name 'isnull' is not defined

I also saw that someone just replaced the "None" strings, but neither of these variations on that approach worked for me: Rename "None" value in Pandas

我还看到有人刚刚替换了“None”字符串,但该方法的这些变体都不适合我: 在 Pandas 中重命名“None”值

yes_records_sample['name'].replace('None', "--no value--")
yes_records_sample['name'].replace(None, "--no value--")

I was ultimately able to use the fillnafunction and fill each of those rows with an empty string yes_records_sample.fillna('')as a workaround and then I could check yes_records_sample['name']==''But I am profoundly confused by how 'None' works and what it means. Is there a way to easily just check if a cell in a dataframe is 'None'?

我最终能够使用该fillna函数并用空字符串填充每一行yes_records_sample.fillna('')作为解决方法,然后我可以检查yes_records_sample['name']==''但我对“无”的工作方式及其含义深感困惑。有没有办法轻松检查数据框中的单元格是否为“无”?

采纳答案by Korem

Call it like this:

像这样调用它:

yes_records_sample['name'].isnull()

回答by g.a

I couldn't find any built-in which does exactly this, so I do it manually. In case of Series, the code is this:

我找不到任何可以做到这一点的内置程序,所以我手动完成。在系列的情况下,代码是这样的:

import numpy as np
series = yes_records_sample['name']
n = np.empty_like(series)
n[...] = None
nones = series.values == n

In case of DataFrames, the code is very similar:

在 DataFrames 的情况下,代码非常相似:

import numpy as np
df = yes_records_sample
n = np.empty_like(df)
n[...] = None
nones = df == n

My problem with .isnull() is that it does not distinguish between NaN and None. This may or may not be a problem in your application.

我对 .isnull() 的问题是它不区分 NaN 和 None。这在您的应用程序中可能是也可能不是问题。