Python 检查 Pandas 中的单个单元格值是否为 NaN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27754891/
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
Check if single cell value is NaN in Pandas
提问by vidit
I just want to check if a single cell in Pandas series is null or not i.e. to check if a value is NaN
.
我只想检查 Pandas 系列中的单个单元格是否为空,即检查值是否为NaN
.
All other answers are for series and arrays, but not for single value.
所有其他答案适用于系列和数组,但不适用于单个值。
I have tried pandas.notnull
, pandas.isnull
, numpy.isnan
. Is there a solution for a single value only?
我已经试过pandas.notnull
,pandas.isnull
,numpy.isnan
。是否有仅针对单个值的解决方案?
采纳答案by Mir Shahriar Sabuj
Try this:
尝试这个:
import pandas as pd
import numpy as np
from pandas import *
>>> L = [4, nan ,6]
>>> df = Series(L)
>>> df
0 4
1 NaN
2 6
>>> if(pd.isnull(df[1])):
print "Found"
Found
>>> if(np.isnan(df[1])):
print "Found"
Found
回答by yashu vishnalia
STEP 1.)
第1步。)
df[df.isnull().any(1)]
----> Will give you dataframe with rows and column, if any value there is nan.
----> 会给你带有行和列的数据框,如果有任何值的话。
STEP 2.)
第2步。)
this will give you location in dataframe where exactly value is nan. then you could do
这将为您提供数据框中的位置,其中确切的值为 nan。那么你可以做
if(**df.iloc[loc_row,loc_colum]==np.nan**):
print"your code here"
回答by sparrow
You can use "isnull" with "at" to check a specific value in a dataframe.
您可以使用“isnull”和“at”来检查数据框中的特定值。
For example:
例如:
import pandas as pd
import numpy as np
df = pd.DataFrame([[np.nan, 2], [1, 3], [4, 6]], columns=['A', 'B'])
Yeilds:
产量:
A B
0 NaN 2
1 1.0 3
2 4.0 6
To check the values:
要检查值:
pd.isnull(df.at[0,'A'])
-> True
-> 真
pd.isnull(df.at[0,'B'])
-> False
-> 错误