pandas判断缺失值的办法

时间:2024-04-11 11:04:37

参考这篇文章:

https://blog.csdn.net/u012387178/article/details/52571725

python pandas判断缺失值一般采用 isnull(),然而生成的却是所有数据的true/false矩阵,对于庞大的数据dataframe,很难一眼看出来哪个数据缺失,一共有多少个缺失数据,缺失数据的位置。

比如:

  0.520113  0.884000  1.260966 -0.236597  0.312972 -0.196281
-0.837552 NaN 0.143017 0.862355 0.346550 0.842952
-0.452595 NaN -0.420790 0.456215 1.203459 0.527425
0.317503 -0.917042 1.780938 -1.584102 0.432745 0.389797
-0.722852 1.704820 -0.113821 -1.466458 0.083002 0.011722
-0.622851 -0.251935 -1.498837 NaN 1.098323 0.273814
0.329585 0.075312 -0.690209 -3.807924 0.489317 -0.841368
-1.123433 -1.187496 1.868894 -2.046456 -0.949718 NaN
1.133880 -0.110447 0.050385 -1.158387 0.188222 NaN
-0.513741 1.196259 0.704537 0.982395 -0.585040 -1.693810

df.isnull().any()则会判断哪些”列”存在缺失值

    False
True
False
True
False
True
dtype: bool

df[df.isnull().values==True]

可以只显示存在缺失值的行列,清楚的确定缺失值的位置。

Out[]: 

  1.090872       NaN -0.287612 -0.239234 -0.589897  1.849413
-1.384721 NaN -0.158293 0.011798 -0.564906 -0.607121
-0.477590 -2.696239 0.312837 NaN 0.404196 -0.797050
0.369665 -0.268898 -0.344523 -0.094436 0.214753 NaN
-0.114483 -0.842322 0.164269 -0.812866 -0.601757 NaN