Python pandas: check if any value is NaN in DataFrame
# 查看每一列是否有NaN:
df.isnull().any(axis=0)
# 查看每一行是否有NaN:
df.isnull().any(axis=1)
# 查看所有数据中是否有NaN最快的:
df.isnull().values.any()
# In [2]: df = pd.DataFrame(np.random.randn(1000,1000))
In [3]: df[df > 0.9] = pd.np.nan
In [4]: %timeit df.isnull().any().any()
100 loops, best of 3: 14.7 ms per loop
In [5]: %timeit df.isnull().values.sum()
100 loops, best of 3: 2.15 ms per loop
In [6]: %timeit df.isnull().sum().sum()
100 loops, best of 3: 18 ms per loop
In [7]: %timeit df.isnull().values.any()
1000 loops, best of 3: 948 µs per loop
# df.isnull().sum().sum() is a bit slower, but of course, has additional information -- the number of NaNs.