在熊猫数据框中检查无

问题描述:

我想找到在数据框中没有找到的地方.

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.

您可以将applymaplambda结合使用,以检查element is None是否如下所示(与原始示例中的示例不同, None被强制为np.nan,因为数据类型为float,您将需要一个object类型列以按原样保存None,或者如@Evert所注释,NoneNaN是在数字类型列中无法区分):

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