检查熊猫的NaN和不同类型

时间:2019-10-26 12:52:23

标签: python-3.x pandas

为了检查大熊猫是否包含缺失/ nan值,可以使用isnull函数。

test_pandas = pd.DataFrame([[np.float(3),np.float(1),np.float(4.3)],[np.float(5.8),np.nan,[1,2,3]]],columns = ['A','B','C'])
value = test_pandas.isnull().values.any()
test_pandas.head()

给予

     A    B          C
0  3.0  1.0        4.3
1  5.8  NaN  [1, 2, 3]

print("There exists a nan value in the dataframe: ",test_pandas.isnull().values.any())
print("Number of nan values: ",test_pandas.isnull().sum().sum())

我们找到

There exists a nan value in the dataframe:  True
Number of nan values:  1
  • 如何以相同的方式检查熊猫数据框中是否存在值,但其类型不是 例如浮动。我想检测一下,在浮点值之间有一个类型列表条目。

1 个答案:

答案 0 :(得分:3)

您可以将自定义函数与applymap一起使用:

def isfloat(x):
    return isinstance(x, float)


print(df.applymap(isfloat))

输出

      A     B      C
0  True  True   True
1  True  True  False