如何用pandas处理缺失值

对Series和单变量情况的缺失值判断

import numpy as np
#这里是之前的代码......
#若要判断变量Number是不是nan
np.isnan(number)

对dataframe缺失值的判断

缺失值在统计分析中经常被用到,在R语言中,is.na()、is.nan()和is.infinite()可分别用来识别缺失值、不可能值和无穷值。在python中空值为None, 在java中空值为null,但是到pandas中空值被显示为NaN。另外,pandas中使用df.isnull()或者df.isna()来判断是否为缺失值。
在这里插入图片描述
在这里插入图片描述

将dataframe含有NaN的行或列去掉

在pandas中可以使用df.dropna(axis=0)或者df.dropna(axis=1)将带有缺失值的行列去掉
在这里插入图片描述

对缺失值进行简单填充

df.fillna(0)#用0填补所有缺失值
df.fillna(method=‘ffill’) #用前一个值填充
df.fillna(method=‘bfill’) #用后一个值填充
df.fillna({‘a’:100,‘b’:200,‘c’:300})#不同的列用不同的值填充

对缺失值进行插补

在旧版的sklearn中,利用skearn.preprocessing中的Imputer方法可以对数据进行插补。
在新版的sklearn中,调用impute里面的SimpleImputer函数。

class sklearn.imputer.SimpleImputer(missing_values=’NaN’, strategy=’mean’, axis=0, verbose=0, copy=True)

参数:

missing_values: integer or “NaN”, optional (default=”NaN”)
strategy : string, optional (default=”mean”)
The imputation strategy.
If “mean”, then replace missing values using the mean along the axis. 使用平均值代替
If “median”, then replace missing values using the median along the axis.使用中值代替
If “most_frequent”, then replace missing using the most frequent value along the axis.使用众数代替,也就是出现次数最多的数
axis: 默认为 axis=0
axis = 0, 按列处理
aixs =1 , 按行处理

注意:

Imputer 只接受DataFrame类型
Dataframe 中必须全部为数值属性

可以参考:
https://blog.csdn.net/dss_dssssd/article/details/82831240?depth_1-

在这里插入图片描述

未完待续。。。。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值