Pandas处理缺失的数据
处理丢失数据
有两种丢失数据:
- None
- np.nan(NaN)
import numpy as np
import pandas
from pandas import DataFrame
1. None
None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。
# 查看None的数据类型
type(None)
NoneType
2. np.nan(NaN)
np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。
# 查看np.nan的数据类型
type(np.nan)
float
3. pandas中的None与NaN
创建DataFrame
df = DataFrame(data=np.random.randint(0,100,size=(10,8)))
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| 0 | 22 | 13 | 16 | 41 | 81 | 7 | 25 | 86 |
| 1 | 23 | 3 | 57 | 20 | 4 | 58 | 69 | 40 |
| 2 | 35 | 81 | 80 | 63 | 53 | 43 | 20 | 35 |
| 3 | 40 | 14 | 48 | 89 | 34 | 4 | 64 | 46 |
| 4 | 36 | 14 | 62 | 30 | 80 | 99 | 88 | 59 |
| 5 | 9 | 98 | 83 | 81 | 69 | 46 | 39 | 7 |
| 6 | 55 | 88 | 81 | 75 | 35 | 44 | 27 | 64 |
| 7 | 14 | 74 | 24 | 3 | 54 | 99 | 75 | 53 |
| 8 | 24 | 22 | 41 | 68 | 1 | 87 | 46 | 19 |
| 9 | 82 | 10 | 36 | 99 | 85 | 36 | 12 | 83 |
# 将某些数组元素赋值为nan
df.iloc[1,4] = None
df.iloc[3,6] = None
df.iloc[7,7] = None
df.iloc[3,1] = None
df.iloc[5,5] = np.nan
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| 0 | 22 | 13.0 | 16 | 41 | 81.0 | 7.0 | 25.0 | 86.0 |
| 1 | 23 | 3.0 | 57 | 20 | NaN | 58.0 | 69.0 | 40.0 |
| 2 | 35 | 81.0 | 80 | 63 | 53.0 | 43.0 | 20.0 | 35.0 |
| 3 | 40 | NaN | 48 | 89 | 34.0 | 4.0 | NaN | 46.0 |
| 4 | 36 | 14.0 | 62 | 30 | 80.0 | 99.0 | 88.0 | 59.0 |
| 5 | 9 | 98.0 | 83 | 81 | 69.0 | NaN | 39.0 | 7.0 |
| 6 | 55 | 88.0 | 81 | 75 | 35.0 | 44.0 | 27.0 | 64.0 |
| 7 | 14 | 74.0 | 24 | 3 | 54.0 | 99.0 | 75.0 | NaN |
| 8 | 24 | 22.0 | 41 | 68 | 1.0 | 87.0 | 46.0 | 19.0 |
| 9 | 82 | 10.0 | 36 | 99 | 85.0 | 36.0 | 12.0 | 83.0 |
pandas处理空值操作
判断函数
isnull()notnull()
df.isnull() # 为空,显示True
df.notnull() # 不为空,显示True
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| 0 | True | True | True | True | True | True | True | True |
| 1 | True | True | True | True | False | True | True | True |
| 2 | True | True | True | True | True | True | True | True |
| 3 | True | False | True | True | True | True | False | True |
| 4 | True | True | True | True | True | True | True | True |
| 5 | True | True | True | True | True | False | True | True |
| 6 | True | True | True | True | True | True | True | True |
| 7 | True | True | True | True | True | True | True | False |
| 8 | True | True | True | True | True | True | True | True |
| 9 | True | True | True | True | True | True | True | True |
- df.notnull/ isnull().any()/ all()
df.isnull()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| 0 | False | False | False | False | False | False | False | False |
| 1 | False | False | False | False | True | False | False | False |
| 2 | False | False | False | False | False | False | False | False |
| 3 | False | True | False | False | False | False | True | False |
| 4 | False | False | False | False | False | False | False | False |
| 5 | False | False | False | False | False | True | False | False |
| 6 | False | False | False | False | False | False | False | False |
| 7 | False | False | False | False | False | False | False | True |
| 8 | False | False | False | False | False | False | False | False |
| 9 | False | False | False | False | False | False | False | False |
df.isnull().any(axis=1) # any表示or,axis=1表示行,即一行中存在True,即为True
0 False
1 True
2 False
3 True
4 False
5 True
6 False
7 True
8 False
9 False
dtype: bool
df.notnull().all(axis=1) # all表示and,axis=1表示行,即一行中全为True,才为True
0 True
1 False
2 True
3 False
4 True
5 False
6 True
7 False
8 True
9 True
dtype: bool
df.loc[~df.isnull().any(axis=1)] # ~表示取反
往往这样搭配:
- isnull()->any
- notnull()->all
df.dropna() 可以选择过滤的是行还是列(默认为行):axis中0表示行,1表示的列
df.dropna(axis=0)
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| 0 | 22 | 13.0 | 16 | 41 | 81.0 | 7.0 | 25.0 | 86.0 |
| 2 | 35 | 81.0 | 80 | 63 | 53.0 | 43.0 | 20.0 | 35.0 |
| 4 | 36 | 14.0 | 62 | 30 | 80.0 | 99.0 | 88.0 | 59.0 |
| 6 | 55 | 88.0 | 81 | 75 | 35.0 | 44.0 | 27.0 | 64.0 |
| 8 | 24 | 22.0 | 41 | 68 | 1.0 | 87.0 | 46.0 | 19.0 |
| 9 | 82 | 10.0 | 36 | 99 | 85.0 | 36.0 | 12.0 | 83.0 |
填充函数 Series/DataFrame
fillna():value和method参数
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| 0 | 22 | 13.0 | 16 | 41 | 81.0 | 7.0 | 25.0 | 86.0 |
| 1 | 23 | 3.0 | 57 | 20 | NaN | 58.0 | 69.0 | 40.0 |
| 2 | 35 | 81.0 | 80 | 63 | 53.0 | 43.0 | 20.0 | 35.0 |
| 3 | 40 | NaN | 48 | 89 | 34.0 | 4.0 | NaN | 46.0 |
| 4 | 36 | 14.0 | 62 | 30 | 80.0 | 99.0 | 88.0 | 59.0 |
| 5 | 9 | 98.0 | 83 | 81 | 69.0 | NaN | 39.0 | 7.0 |
| 6 | 55 | 88.0 | 81 | 75 | 35.0 | 44.0 | 27.0 | 64.0 |
| 7 | 14 | 74.0 | 24 | 3 | 54.0 | 99.0 | 75.0 | NaN |
| 8 | 24 | 22.0 | 41 | 68 | 1.0 | 87.0 | 46.0 | 19.0 |
| 9 | 82 | 10.0 | 36 | 99 | 85.0 | 36.0 | 12.0 | 83.0 |
# bfill表示后, ffill表示前
# axis表示方向: 0:上下, 1:左右
df_test = df.fillna(method='bfill',axis=1).fillna(method='ffill',axis=1)
df_test
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| 0 | 22.0 | 13.0 | 16.0 | 41.0 | 81.0 | 7.0 | 25.0 | 86.0 |
| 1 | 23.0 | 3.0 | 57.0 | 20.0 | 58.0 | 58.0 | 69.0 | 40.0 |
| 2 | 35.0 | 81.0 | 80.0 | 63.0 | 53.0 | 43.0 | 20.0 | 35.0 |
| 3 | 40.0 | 48.0 | 48.0 | 89.0 | 34.0 | 4.0 | 46.0 | 46.0 |
| 4 | 36.0 | 14.0 | 62.0 | 30.0 | 80.0 | 99.0 | 88.0 | 59.0 |
| 5 | 9.0 | 98.0 | 83.0 | 81.0 | 69.0 | 39.0 | 39.0 | 7.0 |
| 6 | 55.0 | 88.0 | 81.0 | 75.0 | 35.0 | 44.0 | 27.0 | 64.0 |
| 7 | 14.0 | 74.0 | 24.0 | 3.0 | 54.0 | 99.0 | 75.0 | 75.0 |
| 8 | 24.0 | 22.0 | 41.0 | 68.0 | 1.0 | 87.0 | 46.0 | 19.0 |
| 9 | 82.0 | 10.0 | 36.0 | 99.0 | 85.0 | 36.0 | 12.0 | 83.0 |
# 测试df_test中的哪些列中还有空值
df_test.isnull().any(axis=0)
0 False
1 False
2 False
3 False
4 False
5 False
6 False
7 False
dtype: bool
Pandas处理缺失的数据的更多相关文章
- 利用Python进行数据分析-Pandas(第五部分-数据规整:聚合、合并和重塑)
在许多应用中,数据可能分散在许多文件或数据库中,存储的形式也不利于分析.本部分关注可以聚合.合并.重塑数据的方法. 1.层次化索引 层次化索引(hierarchical indexing)是panda ...
- 实操 | 内存占用减少高达90%,还不用升级硬件?没错,这篇文章教你妙用Pandas轻松处理大规模数据
注:Pandas(Python Data Analysis Library) 是基于 NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.此外,Pandas 纳入了大量库和一些标准的数据模型 ...
- 小白学 Python 数据分析(8):Pandas (七)数据预处理
人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...
- 小白学 Python 数据分析(12):Pandas (十一)数据透视表(pivot_table)
人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...
- pandas(七)数据规整化:清理、转换、合并、重塑之合并数据集
pandas对象中的数据可以通过一些内置的方式进行合并: pandas.merge 可根据一个或多个键将不同的DataFrame中的行连接起来. pandas.concat可以沿着一条轴将多个对象堆叠 ...
- 使用Pandas将多个数据表合一
使用Pandas将多个数据表合一 将多张数据表合为一张表,便于统计分析,进行这一操作的前提为这多张数据表互相之间有关联信息,或者有相同的列. import pandas as pd unames = ...
- Python3 Pandas的DataFrame格式数据写入excle文件、json、html、剪贴板、数据库
Python3 Pandas的DataFrame格式数据写入excle文件.json.html.剪贴板.数据库 一.DataFrame格式数据 Pandas是Python下一个开源数据分析的库,它提供 ...
- @1-5使用pandas保存豆瓣短评数据
使用pandas保存豆瓣短评数据 Python爬虫(入门+进阶) DC学院 本节课程的内容是介绍open函数和pandas两种保存已爬取的数据的方法,并通过实际例子使用pandas保存数据. ...
- 使用pandas把mysql的数据导入MongoDB。
使用pandas把mysql的数据导入MongoDB. 首先说下我的需求,我需要把mysql的70万条数据导入到mongodb并去重, 同时在第二列加入一个url字段,字段的值和第三列的值一样,代码如 ...
随机推荐
- jQuery学习总结05-事件
1.事件的发生 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- break语句、continue语句、goto语句的用法辨析
1.break语句 break语句常使用在switch语句.循环体以及if语句中,它的作用是跳出循环,而且只能跳出一层循环. for (i = 0; i < 10; ++j) { for (j ...
- 2019hdu多校3 hdu4893(线段树单点 区间更新
补这题主要是因为第三个操作要维护区间,而不是点,否则会T. https://vjudge.net/problem/HDU-4893 题意:输入n.q.表示有n个数,初始化默认这n个数都为零,有q次操作 ...
- 反射getDeclaredFields()
public static void main(String[] args) { // 获取所有属性值 Field[] fields = People.class.getDeclaredFields( ...
- bzoj1430 小猴打架 prufer 序列
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1430 题解 prufer 序列模板题. 一个由 \(n\) 个点构成的有标号无根树的个数为 \ ...
- JIRA之两大统计图讲解
一.创建与解决的问题-状态统计图 配置方式 理解该统计图 横坐标 x:时间 纵坐标 y:issue数量 统计图示解读: A.随着时间的推移,创建的问题数(红线)减少,修复问题数(绿线)增加,标志着版本 ...
- TFrecords读、写图片文件
参考:https://blog.csdn.net/u014802590/article/details/68495238 参考:https://www.2cto.com/kf/201709/68057 ...
- Oracle分组函数之Grouping Sets
功能介绍: 自定义分组的字段 创建表: 插入测试数据: Grouping Sets(null,t.classid,(t.classid,t.studentname)),类似于ROLLUP Select ...
- 如何从word文档复制内容到富文本编辑器
在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper. 通过知乎提供的思路找到粘贴的原理,通过TheViper找 ...
- js点击获取—通过JS获取图片的相对坐标位置
一.通过JS获取鼠标点击时图片的相对坐标位置 源代码如下所示: <!DOCTYPE html> <html lang="en"> <head> ...