from __future__ import print_function
import pandas as pd
import numpy as np
np.random.seed(1)
dates = pd.date_range('20130101', 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 # Nan模拟缺失的数据
print(df)
print(df.dropna(axis=0, how='all')) # how={'any', 'all'} 全部是0才丢掉
print(df.dropna(axis=0, how='any')) # how={'any', 'all'} 有0就丢掉行
print(df.fillna(value=0)) # 所有的nan用0填充
print(pd.isnull(df)) # 判断每一个数据是否是Nan,是nan就输出True
print(np.any(df.isnull()) == True) # 如果有数据缺失,就输出True
以下是所有的输出结果:
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
print(df.dropna(axis=0, how='all')) # how={'any', 'all'}
> 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
print(df.dropna(axis=0, how='any')) # how={'any', 'all'}
> 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
print(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
print(pd.isnull(df))
> 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
print(np.any(df.isnull()) == True)
> True
END