百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

python数据清洗的三个常用的处理方式!

off999 2024-09-16 00:48 46 浏览 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')

最后,整个数据清洗的过程就完成了,希望可以给大家带来帮助,感谢阅读!

相关推荐

win10开机慢怎么设置(win 10开机太慢)
  • win10开机慢怎么设置(win 10开机太慢)
  • win10开机慢怎么设置(win 10开机太慢)
  • win10开机慢怎么设置(win 10开机太慢)
  • win10开机慢怎么设置(win 10开机太慢)
手机路由器管理(手机路由器管理界面进不去是什么原因)
  • 手机路由器管理(手机路由器管理界面进不去是什么原因)
  • 手机路由器管理(手机路由器管理界面进不去是什么原因)
  • 手机路由器管理(手机路由器管理界面进不去是什么原因)
  • 手机路由器管理(手机路由器管理界面进不去是什么原因)
佳能(中国)官网下载(佳能(中国)官网下载appstore)

需要先进入佳能官网的下载页面,选择手机APP下载选项,根据手机操作系统的不同选择相应的下载链接即可成功下载佳能手机APP。下载链接通常会在网站的首页或者是产品页面上提供。总的来说,下载佳能手机APP非...

c盘右边有个恢复分区怎么删除

1、从网上下载“分区助手专业6.2(或5.6)”,它能无损分区,下载后打开按提示安装,点击分区助手桌面快捷方式图标,打开分区助手专业版6.2主界面。2、右击要调出空间的分区,如E,选“分配自由空间”,...

电脑插着电源却不充电怎么办

电脑插上电源但无法充电可能有以下原因:1.电池没有完全安装,需要检查电池是否完全插入笔记本电脑中。2.电池损坏,如果电池老化或发生机械故障、磨损和损伤,充电电流将会被阻塞从而无法进行充电,需要更换...

如何格式化手机(华为p50如何格式化手机)
如何格式化手机(华为p50如何格式化手机)

步骤/方式1软件格式化:利用psiloc公司的软件sTools,进行格式化手机,锁码为12345步骤/方式2软格:在手机上输入*#7370#之后要求你输入锁码,初始密码是:12345步骤/方式3硬格:先关机,再开机的时候按住拨号键、“*...

2025-12-17 12:03 off999

win10自动更新的禁用方法(win10自动更新的禁用方法是什么)

方法一:Windows设置  要想关闭Win10自动更新,比较简单的一种方法就是进入到Windows设置中,将Windows更新直接关闭。步骤如下:  1、按“Windows+I”键,打开Wind...

优化win7系统运行速度(优化win7系统运行速度多少)

优化WIN7系统开机启动项的操作方法1、在桌面上按组合键(win键+R)打开运行窗口,接着输入“regedit”,回车确认,2、打开注册表编辑器后,我们依次点击展开“HKEY_CURRENT_USE...

win7设置每天自动开机时间(win7设置每天自动开机时间任务)

要在Windows7上设置每天自动开关机,您可以按照以下步骤操作:1.打开“控制面板”,单击“系统和安全”,然后选择“计划任务”。2.单击“创建基本任务”,输入一个适合您的任务名称,并添加相应的...

苹果电脑装双系统好用吗(苹果电脑安装双系统会不会对电脑不好)

好处:1、可以在保留原来的系统上再安装一个新系统,两个系统互不干扰,可以互相切换,使用方便。2、双系统可以在不用环境系进行软件调试没测试电脑的兼容性。3、双系统可以让用户体验不同的系统功能,提高用户的...

qq好友恢复网站官方网站(qq好友恢复官方网站为什么不能用)
  • qq好友恢复网站官方网站(qq好友恢复官方网站为什么不能用)
  • qq好友恢复网站官方网站(qq好友恢复官方网站为什么不能用)
  • qq好友恢复网站官方网站(qq好友恢复官方网站为什么不能用)
  • qq好友恢复网站官方网站(qq好友恢复官方网站为什么不能用)
在电脑上复制粘贴按什么键(电脑怎复制粘贴按那个键)

电脑键盘上的“复制和粘贴”,分别是Ctrl+c和Ctrl+v,其中复制的快捷键是Ctrl+c,粘贴的快捷键是Ctrl+v。鼠标右键,点击右键会出菜单,移动光标后点击左键确认。键盘复制的快捷键:Ctrl...

office是电脑自带的吗(电脑自带的office都是2016版)

基本上大品牌电脑,都会带正版的office软件。如果是自己组装的电脑,一般使用的盗版软件,不是正版的。现在office软件分为国产和进口两个版本,进口的是微软office,国产的是wpsoffice...

怎么样的电脑配置才算好(怎么样的电脑配置才算好的)
  • 怎么样的电脑配置才算好(怎么样的电脑配置才算好的)
  • 怎么样的电脑配置才算好(怎么样的电脑配置才算好的)
  • 怎么样的电脑配置才算好(怎么样的电脑配置才算好的)
  • 怎么样的电脑配置才算好(怎么样的电脑配置才算好的)
appstore应用商店下载(AppStore应用商店下载入口)

可能因为1.你的软件原来在其他国家下载的,你现在账户不支持那个软件的更新,只要更改到相应的地区就好了2.可能你网不好(?˙ー˙?)3.你的pad原来登录的账户和现在不一样,所以你现在...

取消回复欢迎 发表评论: