python数据清洗的三个常用的处理方式!
off999 2024-09-16 00:48 52 浏览 0 评论
关于python数据处理过程中三个主要的数据清洗说明,分别是缺失值/空格/重复值的数据清洗。
这里还是使用pandas来获取excel或者csv的数据源来进行数据处理。若是没有pandas的非标准库需要使用pip的方式安装一下。
pip install pandas
准备一下需要处理的脏数据,这里选用的是excel数据,也可以选择其他的格式数据,下面是源数据截图。
使用pandas的read_excel()函数读取出我们需要处理的data.xlsx文件。
# Importing the pandas library and giving it an alias of pd.
import pandas as pd
# Reading the excel file and storing it in a variable called `result_`
result_ = pd.read_excel('D:/test/data.xlsx')
# Printing the dataframe.
print(result_)
注意,若是新的python环境直接安装pandas模块后执行上面的读取excel数据代码可能会出现没有openpyxl模块的情况。
这时候,我们使用pip的方式再次安装一下openpyxl即可。
pip install openpyxl
完成后再次执行读取excel数据的代码块会成功的返回结果。
# 姓名 年龄 班级 成绩 表现
# 0 Python 集中营 10 1210 99 A
# 1 Python 集中营 11 1211 100 A
# 2 Python 集中营 12 1212 101 A
# 3 Python 集中营 13 1213 102 A
# 4 Python 集中营 14 1214 103 A
# 5 Python 集中营 15 1215 104 A
# 6 Python 集中营 16 1216 105 A
# 7 Python 集中营 17 1217 106 A
# 8 Python 集中营 18 1218 107 A
# 9 Python 集中营 19 1219 108 A
# 10 Python 集中营 20 1220 109 A
# 11 Python 集中营 21 1221 110 A
# 12 Python 集中营 22 1222 111 A
# 13 Python 集中营 23 1223 112 A
# 14 Python 集中营 24 1224 113 A
# 15 Python 集中营 25 1225 114 A
# 16 Python 集中营 26 1226 115 A
# 17 Python 集中营 27 1227 116 A
# 18 Python 集中营 28 1228 117 A
#
# Process finished with exit code 0
准备好数据源之后,我们使用三个方式来完成对源数据的数据清洗。
1. strip函数清除空格
首先,将所有的列名称提取出来,使用DataFrame对象的columns函数进行提取。
# Extracting the column names from the dataframe and storing it in a variable called `columns_`.
columns_ = result_.columns.values
# Printing the column names of the dataframe.
print(columns_)
# [' 姓名 ' '年龄' '班级' '成绩' '表现']
从列名称的打印结果发现'姓名'这一列是存在空格的,我们直接查找列名称是找不到的,因为需要对列名称的空格进行数据清洗。
为了减少代码块的使用,我们这里直接使用列表推导式的方式对列名称的空格进行清洗。
# A list comprehension that is iterating over the `columns_` list and stripping the whitespaces from each element of the
# list.
result_.columns = [column_name.strip() for column_name in columns_]
# Printing the column names of the dataframe.
print(result_.columns.values)
# ['姓名' '年龄' '班级' '成绩' '表现']
经过数据清洗后,发现所有的列名称空格情况已经被全部清洗了。若是存在某个列中的值空格需要清洗也可以采用strip函数进行清洗。
2. duplicated函数清除重复数据
关于重复数据的判断有两种情况,一种是两行完全相同的数据即为重复数据。另外一种则是部分相同指的是某个列的数据是相同的需要清洗。
# The `duplicated()` function is returning a boolean series that is True if the row is a duplicate and False if the row is
# not a duplicate.
repeat_num = result_.duplicated().sum()
# Printing the number of duplicate rows in the dataframe.
print(repeat_num)
# 1
通过上面的duplicated().sum()函数得到的是两个完全相同的数据行是多少。
接着则可以对源数据进行实际意义上的删除,使用DataFrame对象的drop_duplicates函数进行删除。
# The `drop_duplicates()` function is dropping the duplicate rows from the dataframe and the `inplace=True` is
# modifying the dataframe in place.
result_.drop_duplicates(inplace=True)
# Printing the dataframe.
print(result_)
# 姓名 年龄 班级 成绩 表现
# 0 Python 集中营 10 1210 99 A
# 1 Python 集中营 11 1211 100 A
# 2 Python 集中营 12 1212 101 A
# 3 Python 集中营 13 1213 102 A
# 4 Python 集中营 14 1214 103 A
# 5 Python 集中营 15 1215 104 A
# 6 Python 集中营 16 1216 105 A
# 7 Python 集中营 17 1217 106 A
# 8 Python 集中营 18 1218 107 A
# 9 Python 集中营 19 1219 108 A
# 10 Python 集中营 20 1220 109 A
# 11 Python 集中营 21 1221 110 A
# 12 Python 集中营 22 1222 111 A
# 13 Python 集中营 23 1223 112 A
# 14 Python 集中营 24 1224 113 A
# 15 Python 集中营 25 1225 114 A
# 16 Python 集中营 26 1226 115 A
# 17 Python 集中营 27 1227 116 A
因为最后一行和第一行的数据是完全相同的,因此最后一行的数据已经被清洗掉了。
一般在数据清洗删除重复值之后需要重置索引,避免索引产生不连续性。
# The `range(result_.shape[0])` is creating a list of numbers from 0 to the number of rows in the dataframe.
result_.index = range(result_.shape[0])
# The `print(result_.index)` is printing the index of the dataframe.
print(result_.index)
# RangeIndex(start=0, stop=18, step=1)
3. 数据缺失值补全
一般查看DataFrame数据对象的缺失值就是通过使用isnull函数来提取所有数据缺失的部分。
# The `isnull()` function is returning a boolean series that is True if the value is missing and False if the value
# is not missing.
sul_ = result_.isnull()
# The `print(sul_)` is printing the boolean series that is True if the value is missing and False if the value is not
# missing.
print(sul_)
# 姓名 年龄 班级 成绩 表现
# 0 False False False False False
# 1 False False False False False
# 2 False False False False False
# 3 False False False False False
# 4 False False False False False
# 5 False False False False False
# 6 False False False False False
# 7 False False False False False
# 8 False False False False False
# 9 False False False False False
# 10 False False False False False
# 11 False False False False False
# 12 False False False False False
# 13 False False False False False
# 14 False False False False False
# 15 False False False False False
# 16 False False False False False
# 17 False False False False False
返回的每一个单元格数据结果为False则代表这个单元格的数据是没有缺失的,或者也可以使用notnull来反向查看。
使用isnull函数不想显示很多的列表数据时,可以使用sum函数进行统计。
# The `isnull_sum = result_.isnull().sum()` is returning a series that is the sum of the boolean series that is True if
# the value is missing and False if the value is not missing.
isnull_sum = result_.isnull().sum()
# The `isnull_sum = result_.isnull().sum()` is returning a series that is the sum of the boolean series that is True if
# the value is missing and False if the value is not missing.
print(isnull_sum)
# 姓名 0
# 年龄 0
# 班级 0
# 成绩 0
# 表现 0
# dtype: int64
通过isnull函数处理后使用sum函数进行统计,统计后会返回每一列的数据单元格为空的个数。
接下来就是数据值的填补过程,通常可以筛选每一列中的空值填补固定的数据。
# The `result_.loc[result_.姓名.isnull(), '姓名']` is returning a series that is the values of the column `姓名`
# where the values are missing. The `'Python 集中营'` is the value that is being assigned to the series.
result_.loc[result_.姓名.isnull(), '姓名'] = 'Python 集中营'
# Printing the dataframe.
print(result_)
# 姓名 年龄 班级 成绩 表现
# 0 Python 集中营 10 1210 99 A
# 1 Python 集中营 11 1211 100 A
# 2 Python 集中营 12 1212 101 A
# 3 Python 集中营 13 1213 102 A
# 4 Python 集中营 14 1214 103 A
# 5 Python 集中营 15 1215 104 A
# 6 Python 集中营 16 1216 105 A
# 7 Python 集中营 17 1217 106 A
# 8 Python 集中营 18 1218 107 A
# 9 Python 集中营 19 1219 108 A
# 10 Python 集中营 20 1220 109 A
# 11 Python 集中营 21 1221 110 A
# 12 Python 集中营 22 1222 111 A
# 13 Python 集中营 23 1223 112 A
# 14 Python 集中营 24 1224 113 A
# 15 Python 集中营 25 1225 114 A
# 16 Python 集中营 26 1226 115 A
# 17 Python 集中营 27 1227 116 A
4. 数据保存
数据清洗完成之后,可以使用DataFrame对象提供的to_csv/to_excel等函数进行特定格式的数据保存。
result_.to_excel('data.xlsx')
最后,整个数据清洗的过程就完成了,希望可以给大家带来帮助,感谢阅读!
相关推荐
-
- cad2014破解版激活教程(cad 2014破解版怎么样激活)
-
步骤如下:1.打开CAD2014,点击激活,勾选同意协议之后它会告诉您,您的序列号是错误的,这时点击关闭等一会再点击激活即可;2.在激活界面中选择“我拥有一个Autodesk激活码”;3.启动对应版本的XFORCEKeygen32bit...
-
2026-02-03 15:51 off999
- electricity(electricity翻译)
-
electricity[英][??lek?tr?s?ti][美][?l?k?tr?s?ti,?il?k-]n.电力;电流,静电;高涨的情绪;紧张;以上结果来自金山词霸例句:1.Article31pow...
- 腾讯游戏实名认证中心官网(腾讯游戏实名认证官网首页)
-
QQ游戏实名注册和防沉迷系统设置方法:第一步:登录实名注册和防沉迷系统网站(http://jkyx.qq.com/web2010/authoriz.htm);第二步:填写实名制信息;第三步:等待审核,...
- qlv文件怎么转换成mp4(qlv文件怎么转换成mp4格式)
-
要将QLV文件转换为MP4文件,您可以使用专业的视频转换软件。首先,下载并安装一个可靠的视频转换器,如Handbrake或AnyVideoConverter。然后,打开软件并导入您要转换的QLV文...
- 央视网cctv5直播(央视网cctv5直播在线观看)
-
看CCTV5直播可以直接选择网页看直播或者使用播放器看直播频道。1、打开e网站2、打开我打圈的,电视直播,但是这里面有些台没有,比如浙江卫视3、然后在左边可以选择你想看的台4、方法2,打开网络主流播放...
- qq历史版本官方下载(qq历史旧版本大全)
-
蟹妖。我用的小米手机,可以双开两个应用,你可以搞两个一样版本的qq。小米手机,还可以手机分身,一个手机不同的解锁方式进入不同的系统,第一个系统你可以安最新版本的qq,另一个系统你可以安装其他版...
- 一个人看的片免费高清大全(一个人看的片免费高清大全在线观看)
-
推荐“爱奇艺App手机版”。它是百度旗下的高清电影站,有许多最新最热的正版大片供免费观看,爱奇艺APP是一款集视频、商城、购票、阅读、直播、泡泡粉丝社区等多种服务于一体的移动软件。爱奇艺手机版为用户提...
- 搜狗翻译app下载(搜狗翻译app下载安装)
-
有啊,点击输入框弹出搜狗键盘,进入搜狗工具栏最左边的图标,进入加号,添加中英互译。1、首先确认是否电脑键盘按键出现问题,可以通过win+r调出运行,输入osk。2、调出虚拟键盘测试虚拟键盘是否可以正常...
- 女生付费和男生聊天(女生找男生聊天掉价吗)
-
不管在什么情况下女生好像都处在优势,因为只听说过娶不到媳妇儿的单身汉,还没有听说过嫁不出去的丑媳妇儿。所以一般这种交友聊天软件就是奔着让男人出钱来的,而且这类软件骗子太多,几乎都是机器人,或者是各种托...
- 腾讯公司版权所有(腾讯开放版权)
-
你好!其实这个很好理解,就是版权公司把这个音乐的管理、销售权限(独家版权)授权给了腾讯,腾讯可以把音乐对外进行转授权。所以你看有一些腾讯独家版权的歌,为什么其他音乐平也台会有呢?其实就是腾讯授予的。因...
- 智能语音机器人(智能语音机器人哪个最好)
-
1、小米的小爱小爱的智能应用随着近年来的不断升级,在很多手机应用都带来了智能新体验。像移动支付、生活购物、查询信息、打开应用等等,小爱智能语音识别就可以帮你解决。用户还可以自己创建快捷方式,进行语音交...
- adobe lightroom(adobe lightroom for ipad破解版)
-
adobelightroom是一款非常不错的在线教育软件,adobelinghtroom这款软件具有资源管理,资源共享,在线学习,在线考试,成绩管理,教学管控,教学互动,效果评估等等功能,可以为用户提...
- 解压软件免费(电脑解压软件免费)
-
WinRAR压缩软件winrar压缩软件界面友好,WINRAR现在最好的压缩工具。使用方便,压缩率和速度方面都有很好的表示。其压缩率比之WINZIP之流要高。winrar压缩软件采用了比Zip更...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
win7系统还原步骤图解(win7还原电脑系统的步骤)
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
python入门到脱坑 输入与输出—str()函数
-
16949认证费用是多少(16949审核员太难考了)
-
linux软件(linux软件图标)
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
