pandas检查和填充缺失值的N种方法总结
作者:爱挠静香的下巴 时间:2021-03-27 03:25:21
一、构建示例数据
import pandas as pd
import numpy as np
data = {"ID":[202001, 202002, 202003, 202004, 202005, 202006, 202007, 202008, 202009, 202010],
"Chinese":[98, 67, 84, 88, 78, 90, 93, np.nan, 82, 87],
"Math":[92, 80, 73, np.nan, 88, 78, 90, 82, 77, 69],
"English":[88, 79, 90, 73, 79, 83, 81, np.nan, 71, np.nan]
}
df = pd.DataFrame(data)
df
二、检查缺失值的n种方法
2.1 确认是否有缺失值的两种方法
df.isnull().values.any()
True
df.isnull().sum().any()
True
2.2 查看缺失数目和缺失率
df.isnull().sum()
all_data_na = (df.isnull().sum()/len(df))*100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index).sort_values(ascending=False)
missing_data = pd.DataFrame({'缺失率' : all_data_na})
missing_data
2.3 查看非缺失值数目
df.info()
df.shape[0] - df.isnull().sum()
df.notnull().sum()
三、缺失值填充三种示例
# 用上下平均值填充English
df['English'] = df['English'].fillna(df['English'].interpolate())
df.head(10)
# 用中位数填充value列:
df['Math'] = df['Math'].fillna(df['Math'].median())
df.head(10)
# 用-1填充Chinese列:
df['Chinese'] = df['Chinese'].fillna(-1)
df.head(10)
来源:https://blog.csdn.net/zzpl139/article/details/128613459
标签:pandas,缺失值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python pyhs2 的安装操作
2021-03-11 18:21:33
PHP数组中头部和尾部添加元素的方法(array_unshift,array_push)
2023-06-06 22:45:50
![](https://img.aspxhome.com/file/2023/2/55472_0s.png)
python标准库random模块处理随机数
2023-11-23 16:22:49
Oracle 外连接实现代码
2009-08-08 23:08:00
详解用Python爬虫获取百度企业信用中企业基本信息
2022-07-27 18:49:52
![](https://img.aspxhome.com/file/2023/6/65416_0s.jpg)
什么是Ajax及Ajax的优势
2007-09-07 09:56:00
![](https://img.aspxhome.com/file/UploadPic/up/2007091821580144.jpg)
python中kmeans聚类实现代码
2023-09-27 19:49:44
用js实现小球的自由移动代码
2023-08-23 02:57:29
![](https://img.aspxhome.com/file/2023/2/56182_0s.png)
ASP中CACHE缓存技术
2010-05-03 10:58:00
python语法学习print中f-string用法示例
2021-01-08 11:11:24
pycharm 实现光标快速移动到括号外或行尾的操作
2023-07-17 19:52:31
关于Ajax在浏览器中产生前进后退的实现方法
2010-01-29 13:01:00
Python 树表查找(二叉排序树、平衡二叉树)
2021-01-27 03:06:20
![](https://img.aspxhome.com/file/2023/8/64318_0s.png)
Node.js和PHP根据ip获取地理位置的方法
2023-11-14 21:23:13
如何正确显示模式对话框showModalDialog中的中文?
2010-06-28 18:24:00
使用python的pexpect模块,实现远程免密登录的示例
2022-10-19 18:30:22
在Python中使用itertools模块中的组合函数的教程
2023-11-06 16:31:36
正则表达式不匹配某个字符串
2010-03-02 22:08:00
Python 下载Bing壁纸的示例
2023-11-20 00:14:41
![](https://img.aspxhome.com/file/2023/8/64598_0s.png)
可能被你忽略的 JavaScript 代码陷阱
2009-12-26 18:16:00