1、创建含NaN的矩阵
>>> dates = pd.date_range('', periods=6)
>>> df = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates, columns=['A','B','C','D'])
>>> df.iloc[0,1] = np.nan
>>> df.iloc[1,2] = np.nan
>>> print(df)
A B C D
2013-01-01 0 NaN 2.0 3
2013-01-02 4 5.0 NaN 7
2013-01-03 8 9.0 10.0 11
2013-01-04 12 13.0 14.0 15
2013-01-05 16 17.0 18.0 19
2013-01-06 20 21.0 22.0 23
2、pd.dropna():直接去掉有NaN的行或列
>>> df.dropna(
... axis=0, # 0: 对行进行操作; 1: 对列进行操作
... how='any' # 'any': 只要存在 NaN 就 drop 掉; 'all': 必须全部是 NaN 才 drop
... )
A B C D
2013-01-03 8 9.0 10.0 11
2013-01-04 12 13.0 14.0 15
2013-01-05 16 17.0 18.0 19
2013-01-06 20 21.0 22.0 23
3、pd.fillna():将NaN的值用其他值代替,比如代替成0:
>>> df.fillna(value=0)
A B C D
2013-01-01 0 0.0 2.0 3
2013-01-02 4 5.0 0.0 7
2013-01-03 8 9.0 10.0 11
2013-01-04 12 13.0 14.0 15
2013-01-05 16 17.0 18.0 19
2013-01-06 20 21.0 22.0 23
4、pd.isnull():判断是否有缺失数据NaN,为True表示缺失数据
>>> df.isnull()
A B C D
2013-01-01 False True False False
2013-01-02 False False True False
2013-01-03 False False False False
2013-01-04 False False False False
2013-01-05 False False False False
2013-01-06 False False False False
>>> np.any(df.isnull()) == True#检测在数据中是否存在 NaN
, 如果存在就返回 True
:
True