【Python】Pandas实现二表查重高亮、去重

导入 Pandas 包

import pandas as pd

输入

读取数据表
#总表
total=pd.read_excel('C:/Users/Kinglake/Desktop/1.xls',index_col=0,header=5)# header=5表示从第5行开始,,且表头为第5行的内容
#total.head()
#新表
new=pd.read_excel('2.xlsx',index_col=0)# 添加index_col=0,可以在读数据时就去除表中默认的index
#new.head()

业务代码

1. 逐行判断 新表的身份证号 是否出现在总表中,返回长度为新表长度的 true false 序列
ifArr=new.身份证号.isin(total.身份证号)
2. 标红样式函数,style.applymap() 需要调用
def color_negative_red(val):
    color = 'red'
    return 'color: %s' % color
3.生成“标红代码”
mycode="new.style"
for i in range(1,len(new)):
    if ifArr[i]:
        #print(i)
        mycode=mycode+'.applymap(color_negative_red,subset=pd.IndexSlice['+str(i)+':'+str(i)+',])'
#print(mycode)

输出

1.执行“标红代码”,生成 hightLightRepeat.xlsx 文件
#添加导出为Excel代码
mycode=mycode+'.to_excel("C:/Users/Kinglake/Desktop/hightLightRepeat.xlsx")'
#执行拼接好的字符串中的代码
exec(mycode) 
2.剔除新表相对于总表的重复项,生成 deleteRepeat.xlsx 文件
new[~new.身份证号.isin(total.身份证号)].to_excel("C:/Users/Kinglake/Desktop/deleteRepeat.xlsx")

所有代码执行完毕

print('---完成!---')
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值