python匹配excel相同的数据_利用Python实现Excel的文件间的数据匹配功能

利用Python实现Excel的文件间的数据匹配功能,字段,在这里,多个,内容,文件名

利用Python实现Excel的文件间的数据匹配功能

易采站长站,站长之家为您整理了利用Python实现Excel的文件间的数据匹配功能的相关内容。

我们知道Excel有一个match函数,可以做数据匹配。

比如要根据人名获取成绩

daefb3805c4bd80e7e1d0778fabd5053.jpg

而参考表sheet1的内容如下:

c992fed84792d1d997f0139c00d42b0a.jpg

要根据sheet1匹配每人的成绩,用Excel是这么写index(Sheet1!B:B,MATCH(A2,Sheet1!A:A,0))

意思就是获取sheet1的B列的内容,根据我的A列匹配sheet1的A列的内容

但是如何用python实现这一点呢,我写了一个函数,非常好用,分享给大家。

这个函数考虑到了匹配多个字段,多个sheet。import pandas as pddef match(file,sheetnames,reffile,refsheet,targetsegs,matchseg) #文件名 sheet列表 参考文件名 参考sheet 目标字段列表 参考字段alldata=pd.read_excel(file,None)refdata=pd.read_excel(reffile,refsheet)#获取映射字典maps={}for i in refdata.index:MatchSeg=refdata.loc[i,matchseg]maps[MatchSeg]={}for seg in targetsegs:maps[MatchSeg][seg]=refdata.loc[i,seg]#匹配数据for sheet in sheetnames:if(isinstance(sheet,int)):sheet=list(alldata.keys())[sheet]data=alldata[sheet].fillna('-')for i in data.index:MatchSeg=data.loc[i,matchseg]for seg in targetsegs:try:data.loc[i,seg]=map[MatchSeg][seg]except Exception as e:passalldata[sheet]=data#导出with pd.ExcelWriter(file) as writer:for sheet in alldata.keys():alldata[sheet].to_excel(writer,sheet,index=False)match('要匹配的表.xlsx',[0,1],'参考表.xlsx','参考页',['要匹配的字段1,字段2'],'参考字段')以上就是关于对利用Python实现Excel的文件间的数据匹配功能的详细介绍。欢迎大家对利用Python实现Excel的文件间的数据匹配功能内容提出宝贵意见

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值