在 pandas 数据框中检查无 [英] Check for None in pandas dataframe
问题描述
我想找到在数据框中没有找到的地方.
I would like to find where None is found in the dataframe.
pd.DataFrame([None,np.nan]).isnull()
OUT:
0
0 True
1 True
isnull()可以找到numpy的Nan和None值.
isnull() finds both numpy Nan and None values.
我只想要None值,而不想要numpy Nan.有没有一种更简单的方法可以做到这一点而无需遍历数据框?
I only want the None values and not numpy Nan. Is there an easier way to do that without looping through the dataframe?
阅读评论后,我意识到我的数据框架中还包含字符串,因此None不会被强制转换为numpy Nan.因此,双鱼座给出的答案有效.
After reading the comments, I realized that in my dataframe in my work also include strings, so the None were not coerced to numpy Nan. So the answer given by Pisdom works.
推荐答案
您可以将applymap
与lambda
结合使用,以检查element is None
是否如下所示(与原始示例中的示例不同, None
被强制为np.nan
,因为数据类型为float
,您将需要一个object
类型列以按原样保存None
,或者如@Evert所注释,None
和NaN
是在数字类型列中无法区分):
You could use applymap
with a lambda
to check if an element is None
as follows, (constructed a different example, as in your original one, None
is coerced to np.nan
because the data type is float
, you will need an object
type column to hold None
as is, or as commented by @Evert, None
and NaN
are indistinguishable in numeric type columns):
df = pd.DataFrame([[None, 3], ["", np.nan]])
df
# 0 1
#0 None 3.0
#1 NaN
df.applymap(lambda x: x is None)
# 0 1
#0 True False
#1 False False
这篇关于在 pandas 数据框中检查无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!