Python 使用ix或iloc检查数据帧中的特定值(单元格中)是否为NaN不工作
假设我有以下Python 使用ix或iloc检查数据帧中的特定值(单元格中)是否为NaN不工作,python,pandas,dataframe,nan,Python,Pandas,Dataframe,Nan,假设我有以下pandasDataFrame: import pandas as pd df = pd.DataFrame({"A":[1,pd.np.nan,2], "B":[5,6,0]}) 这看起来像: >>> df A B 0 1.0 5 1 NaN 6 2 2.0 0 第一选择 我知道一种检查特定值是否为NaN的方法,如下所示: >>> df.isnull().ix[1,0] True 第二个选项(不工作) 我认为下面的选
pandas
DataFrame
:
import pandas as pd
df = pd.DataFrame({"A":[1,pd.np.nan,2], "B":[5,6,0]})
这看起来像:
>>> df
A B
0 1.0 5
1 NaN 6
2 2.0 0
第一选择
我知道一种检查特定值是否为NaN
的方法,如下所示:
>>> df.isnull().ix[1,0]
True
第二个选项(不工作)
我认为下面的选项,使用ix
,也可以,但它不是:
>>> df.ix[1,0]==pd.np.nan
False
我还尝试了iloc
,结果相同:
>>> df.iloc[1,0]==pd.np.nan
False
但是,如果我使用ix
或iloc
检查这些值,我会得到:
>>> df.ix[1,0]
nan
>>> df.iloc[1,0]
nan
那么,为什么第二个选项不起作用?可以使用ix
或iloc
检查NaN
值吗?尝试以下方法:
In [107]: pd.isnull(df.iloc[1,0])
Out[107]: True
更新:在较新版本中使用: 试试这个:
In [107]: pd.isnull(df.iloc[1,0])
Out[107]: True
更新:在较新版本中使用:
以上答案非常好。为了更好地理解,这里也有一个例子
>>> 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
>>>
我也试过几次,以下的试验都不起作用。多亏了@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
>>>
以上答案非常好。为了更好地理解,这里也有一个例子
>>> 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
>>>
我也试过几次,以下的试验都不起作用。多亏了@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
>>>
pd.isna(单元格\u值)
可用于检查给定单元格值是否为nan。或者,pd.notna(cell\u value)
检查相反的值
来自熊猫的源代码:
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
pd.isna(单元格\u值)
可用于检查给定单元格值是否为nan。或者,pd.notna(cell\u value)
检查相反的值
来自熊猫的源代码:
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
说明:试试这个:
pd.np.nan==pd.np.nan
;)这将导致错误
!为什么?这就是“不是数字”的本质。正因为如此,我们在SQL中有了pd.isnull()
,pd.notnull()
,是(非)NULL
,etc@ayhan,您认为如何-我们是否应该将其作为一个dupe关闭?解释:尝试以下操作:pd.np.nan==pd.np.nan
;)这将导致错误
!为什么?这就是“不是数字”的本质。正因为如此,我们在SQL中有了pd.isnull()
,pd.notnull()
,是(非)NULL
,etc@ayhan,你怎么想-我们应该把它当作一个傻瓜来结束吗?太好了。这回答了问题的第二部分。我想另一种方法是pd.isnull(df).iloc[1][0]
@CedricZoppolo,我喜欢你的原始版本(在问题中)-df.isnull().ix[1,0]
better@Cedric另外,np.isnan(df.iloc[1,0])
检查一个数字是否为nan。我将添加df.iloc[1,0]is pd.np.nan
解析为False
,因为类型不同<代码>类型(df.iloc[1,0])解析为
和类型(pd.np.nan)
解析为
,正如@MaxU在问题的评论中所建议的,以检查为什么会发生这种情况。@CedricZoppolo和MaxU不是pd.isnull(df)。iloc[1][0]
效率低得多?太好了。这回答了问题的第二部分。我想另一种方法是pd.isnull(df).iloc[1][0]
@CedricZoppolo,我喜欢你的原始版本(在问题中)-df.isnull().ix[1,0]
better@Cedric另外,np.isnan(df.iloc[1,0])
检查一个数字是否为nan。我将添加df.iloc[1,0]is pd.np.nan
解析为False
,因为类型不同<代码>类型(df.iloc[1,0])解析为
和类型(pd.np.nan)
解析为
,正如@MaxU在问题中的注释所建议的,以检查为什么会发生这种情况。@CedricZoppolo和MaxU不是pd.isnull(df.iloc[1][0]
效率低得多?两个函数pd.isnull
和pd.isna
的作用完全相同。实际上,pd.isnull
是pd.isna
的别名,如中所述。同意,对于初学者来说,isna在使用pandas时更具可读性。这两个函数pd.isnull
和pd.isna
的作用完全相同。实际上,pd.isnull
是pd.isna
的别名,如中所述。同意,对于初学者来说,isna在使用熊猫时更具可读性。