python实现对excel进行数据剔除操作实例
作者:A_DBA_F 时间:2022-09-28 13:53:22
前言
学习Python的过程中,我们会遇到Excel的各种问题。下面这篇文章主要给大家介绍了关于python对excel进行数据剔除操作的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
Python解析Excel时需要安装两个包,分别是xlrd(读excel)和xlwt(写excel),安装方法如下:
pip install xlrd
pip install xlwt
需求分析:
判断excel2表中的某个唯一字段是否满足条件,如果满足条件,就在excel1中进行查询,若存在excel中,就将该数据进行剔除。
python脚本的实现:
from __future__ import division
import pandas as pd
#指定文件的路径
imputfile= 'C:\\Users\\Administrator\\Desktop\\excel1.xlsx' #原始表excel1
imputfile1= 'C:\\Users\\Administrator\\Desktop\\excel2.xls' #excel2
outputfile = 'C:\\Users\\Administrator\\Desktop\\result.xlsx' #结果
#读取excel1的数据到data
data = pd.read_excel(imputfile,encoding='utf-8')
ex_list = list(data.iloc[:,1]) #将需要比对的字段转换为list形式
#读取excel2的数据到remove_data
remove_data = pd.read_excel(imputfile1,encoding='utf-8')
#找出excel2中需要筛选的字段满足的条件。如我这边需要满足的条件是:remove_data.iloc[i,7] =='成功'
remove_phone=[]
for i in range(0,len(remove_data)):
if remove_data.iloc[i,7] =='成功':
phone = remove_data.iloc[i,3]
remove_phone.append(phone)
#删除满足条件数据
for i in range(0,len(remove_phone)):
ex_list.remove(remove_phone[i])
#将剔除后的数据赋值到new_data
new_data=data[data.iloc[:,1].isin(ex_list)]
#导出excel
new_data.to_excel(outputfile)
当然,像这种对excel的剔除数据也可以直接再excel中实现,比如我们先对excel2和excel1都按某一唯一字段进行排序,然后将excel2中需要筛选的结果复制在Excel1中,直接在excel1中根据该字段进行排序。
注意:但是这种方法有一个缺陷是,如果Excel2中的数据并不是完整的,那排序下来也会和excel1不一致。
来源:http://www.cnblogs.com/songdanqi0724/p/7994024.html
标签:python,excel数据,剔除
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在opera里css出现渲染问题
2009-01-15 12:19:00
JS实现点击下拉菜单把选择的内容同步到input输入框内的实例
2023-09-08 19:50:10
![](https://img.aspxhome.com/file/2023/0/56300_0s.jpg)
浅谈javascript的分号[译]
2009-12-13 10:34:00
CSS 裸奔节
2009-04-09 19:36:00
![](https://img.aspxhome.com/file/UploadPic/20094/12/864_001-18s.gif)
《JavaScript语言精粹》译者序及样章试读
2009-04-03 11:38:00
golang逐行读取文件的操作
2023-07-10 14:39:56
php在linux环境中如何使用redis详解
2023-06-14 23:38:30
PHP简单实现HTTP和HTTPS跨域共享session解决办法
2023-11-14 19:39:58
利用Go语言快速实现一个极简任务调度系统
2023-08-28 14:26:15
![](https://img.aspxhome.com/file/2023/7/101687_0s.png)
《色彩解答》系列之一 色彩层次
2008-02-17 14:26:00
![](https://img.aspxhome.com/file/UploadPic/20082/17/2008217143752778s.gif)
谷歌浏览器Chrome的javascript引擎
2008-09-04 12:24:00
![](https://img.aspxhome.com/file/UploadPic/20089/4/200894122755641s.gif)
sql 常用技巧整理
2011-11-03 17:10:14
SQL Server 2005如何设置多字段做关键字
2009-01-08 15:57:00
月影:function扩展
2008-05-19 12:27:00
sql exist的妙用
2007-11-08 11:47:00
使用phpMyAdmin进行mysql数据库备份和还原的方法
2008-10-13 20:56:00
![](https://img.aspxhome.com/file/uploadpic/200810/13/2008101321111543.jpg)
基于python的docx模块处理word和WPS的docx格式文件方式
2021-11-13 12:07:55
python使用matplotlib显示图像失真的解决方案
2021-03-30 22:31:02
![](https://img.aspxhome.com/file/2023/6/73006_0s.jpg)
SQLserver中字符串查找功能patindex和charindex的区别
2012-06-06 20:20:42
Python列表解析操作实例总结
2022-10-10 09:59:38