pandas基础之缺失数据的处理
一、了解缺失的信息
1.isna 和 notna方法
-
对于对Series使用会返回布尔列表
-
对DataFrame使用会返回布尔表,一般和sum()一起使用,了解每一列具体有多少个缺失值
-
df.isna().sum() School 0 Class 4 ID 6 Gender 7 Address 0 Height 0 Weight 13 Math 5 Physics 4 dtype: int64
-
-
通过info()函数也可以查看缺失信息
-
查看缺失值所在的行:可以采用布尔索引
- df[df[‘Physics’].isna()]
-
挑选出所有的非缺失值列
2、三种缺失符号
(1)np.nan
-
np.nan比较麻烦,它不等于任何东西,甚至不等于他自己
-
在numpy中的类型为浮点型,在数据集被读入时,及时原来是整数的列,只要有缺失值就会变成浮点型
-
pd.Series([1,2,3]).dtype # dtype('int64') pd.Series([1,np.nan,3]).dtype # dtype('float64')
-
(2)None
- 布尔值为False
(3)NaT
二、缺失数据的运算
加法与乘法规则
- 使用加法时,缺失值为0
- 使用乘法时,缺失值为1
- 使用累计函数时,缺失值自动跳过