python数据清洗的三个常用的处理方式!
off999 2024-09-16 00:48 35 浏览 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')
最后,整个数据清洗的过程就完成了,希望可以给大家带来帮助,感谢阅读!
相关推荐
- 大文件传不动?WinRAR/7-Zip 入门到高手,这 5 个技巧让你效率翻倍
-
“这200张照片怎么传给女儿?微信发不了,邮箱附件又超限……”62岁的张阿姨对着电脑犯愁时,儿子只用了3分钟就把照片压缩成一个文件,还教她:“以后用压缩软件,比打包行李还方便!”职场人更懂这...
- 电脑解压缩软件推荐——7-Zip:免费、高效、简洁的文件管理神器
-
在日常工作中,我们经常需要处理压缩文件。无论是下载软件包、接收文件,还是存储大量数据,压缩和解压缩文件都成为了我们日常操作的一部分。而说到压缩解压软件,7-Zip绝对是一个不可忽视的名字。今天,我就来...
- 设置了加密密码zip文件要如何打开?这几个方法可以试试~
-
Zip是一种常见的压缩格式文件,文件还可以设置密码保护。那设置了密码的Zip文件要如何打开呢?不清楚的小伙伴一起来看看吧。当我们知道密码想要打开带密码的Zip文件,我们需要用到适用于Zip格式的解压缩...
- 大文件想要传输成功,怎么把ZIP文件分卷压缩
-
不知道各位小伙伴有没有这样的烦恼,发送很大很大的压缩包会受到限制,为此,想要在压缩过程中将文件拆分为几个压缩包并且同时为所有压缩包设置加密应该如何设置?方法一:使用7-Zip免费且强大的文件管理工具7...
- 高效处理 RAR 分卷压缩包:合并解压操作全攻略
-
在文件传输和存储过程中,当遇到大文件时,我们常常会使用分卷压缩的方式将其拆分成多个较小的压缩包,方便存储和传输。RAR作为一种常见的压缩格式,分卷压缩包的使用频率也很高。但很多人在拿到RAR分卷...
- 2个方法教你如何删除ZIP压缩包密码
-
zip压缩包设置了加密密码,每次解压文件都需要输入密码才能够顺利解压出文件,当压缩包文件不再需要加密的时候,大家肯定想删除压缩包密码,或是忘记了压缩包密码,想要通过删除操作将压缩包密码删除,就能够顺利...
- 速转!漏洞预警丨压缩软件Winrar目录穿越漏洞
-
WinRAR是一款功能强大的压缩包管理器,它是档案工具RAR在Windows环境下的图形界面。该软件可用于备份数据,缩减电子邮件附件的大小,解压缩从Internet上下载的RAR、ZIP及其它类...
- 文件解压方法和工具分享_文件解压工具下载
-
压缩文件减少文件大小,降低文件失效的概率,总得来说好处很多。所以很多文件我们下载下来都是压缩软件,很多小伙伴不知道怎么解压,或者不知道什么工具更好,所以今天做了文件解压方法和工具的分享给大家。一、解压...
- [python]《Python编程快速上手:让繁琐工作自动化》学习笔记3
-
1.组织文件笔记(第9章)(代码下载)1.1文件与文件路径通过importshutil调用shutil模块操作目录,shutil模块能够在Python程序中实现文件复制、移动、改名和删除;同时...
- Python内置tarfile模块:读写 tar 归档文件详解
-
一、学习目标1.1学习目标掌握Python内置模块tarfile的核心功能,包括:理解tar归档文件的原理与常见压缩格式(gzip/bz2/lzma)掌握tar文件的读写操作(创建、解压、查看、过滤...
- 使用python展开tar包_python拓展
-
类Unix的系统,打包文件经常使用的就是tar包,结合zip工具,可以方便的打包并解压。在python的标准库里面有tarfile库,可以方便实现生成了展开tar包。使用这个库最大的好处,可能就在于不...
- 银狐钓鱼再升级:白文件脚本化实现GO语言后门持久驻留
-
近期,火绒威胁情报中心监测到一批相对更为活跃的“银狐”系列变种木马。火绒安全工程师第一时间获取样本并进行分析。分析发现,该样本通过阿里云存储桶下发恶意文件,采用AppDomainManager进行白利...
- ZIP文件怎么打开?2个简单方法教你轻松搞定!
-
在日常工作和生活中,我们经常会遇到各种压缩文件,其中最常见的格式之一就是ZIP。ZIP文件通过压缩数据来减少文件大小,方便我们进行存储和传输。然而,对于初学者来说,如何打开ZIP文件可能会成为一个小小...
- Ubuntu—解压多个zip压缩文件.zip .z01 .z02
-
方法将所有zip文件放在同一目录中:zip_file.z01,zip_file.z02,zip_file.z03,...,zip_file.zip。在Zip3.0版本及以上,使用下列命令:将所有zi...
- 如何使用7-Zip对文件进行加密压缩
-
7-Zip是一款开源的文件归档工具,支持多种压缩格式,并提供了对压缩文件进行加密的功能。使用7-Zip可以轻松创建和解压.7z、.zip等格式的压缩文件,并且可以通过设置密码来保护压缩包中的...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 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)