Python 检查特定值(在单元格中)是否在 Pandas DataFrame 中为 NaN 无法使用 ix 或 iloc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47440077/
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
Checking if particular value (in cell) is NaN in pandas DataFrame not working using ix or iloc
提问by Cedric Zoppolo
Lets say I have following pandas
DataFrame
:
可以说我有以下几点pandas
DataFrame
:
import pandas as pd
df = pd.DataFrame({"A":[1,pd.np.nan,2], "B":[5,6,0]})
Which would look like:
看起来像:
>>> df
A B
0 1.0 5
1 NaN 6
2 2.0 0
First option
第一个选项
I know one way to check if a particular value is NaN
, which is as follows:
我知道一种检查特定值是否NaN
为 的方法,如下所示:
>>> df.isnull().ix[1,0]
True
Second option (not working)
第二个选项(不工作)
I thought below option, using ix
, would work as well, but it's not:
我认为下面的选项,使用ix
, 也可以,但事实并非如此:
>>> df.ix[1,0]==pd.np.nan
False
I also tried iloc
with same results:
我也尝试iloc
过相同的结果:
>>> df.iloc[1,0]==pd.np.nan
False
However if I check for those values using ix
or iloc
I get:
但是,如果我使用ix
或检查这些值,iloc
我会得到:
>>> df.ix[1,0]
nan
>>> df.iloc[1,0]
nan
So, why is the second option not working?Is it possible to check for NaN
values using ix
or iloc
?
那么,为什么第二个选项不起作用?是否可以NaN
使用ix
或检查值iloc
?
回答by MaxU
回答by hygull
The above answer is excellent. Here is the same with an example for better understanding.
楼上的回答太好了。为了更好地理解,这里有一个例子。
>>> import pandas as pd
>>>
>>> import numpy as np
>>>
>>> pd.Series([np.nan, 34, 56])
0 NaN
1 34.0
2 56.0
dtype: float64
>>>
>>> s = pd.Series([np.nan, 34, 56])
>>> pd.isnull(s[0])
True
>>>
I also tried couple of times, the following trials did not work. Thanks to @MaxU
.
我也试过几次,下面的试验没有奏效。感谢@MaxU
.
>>> s[0]
nan
>>>
>>> s[0] == np.nan
False
>>>
>>> s[0] is np.nan
False
>>>
>>> s[0] == 'nan'
False
>>>
>>> s[0] == pd.np.nan
False
>>>
回答by Prateek Kumar Dalbehera
pd.isna(cell_value)
can be used to check if a given cell value is nan. Alternatively, pd.notna(cell_value)
to check the opposite.
pd.isna(cell_value)
可用于检查给定的单元格值是否为 nan。或者,pd.notna(cell_value)
检查相反。
From source code of pandas:
来自熊猫的源代码:
def isna(obj):
"""
Detect missing values for an array-like object.
This function takes a scalar or array-like object and indicates
whether values are missing (``NaN`` in numeric arrays, ``None`` or ``NaN``
in object arrays, ``NaT`` in datetimelike).
Parameters
----------
obj : scalar or array-like
Object to check for null or missing values.
Returns
-------
bool or array-like of bool
For scalar input, returns a scalar boolean.
For array input, returns an array of boolean indicating whether each
corresponding element is missing.
See Also
--------
notna : Boolean inverse of pandas.isna.
Series.isna : Detect missing values in a Series.
DataFrame.isna : Detect missing values in a DataFrame.
Index.isna : Detect missing values in an Index.
Examples
--------
Scalar arguments (including strings) result in a scalar boolean.
>>> pd.isna('dog')
False
>>> pd.isna(np.nan)
True