python excel对比,python比较两个excel表并附加正确的记录

博客内容涉及到使用Python的pandas库来合并两个含有序列号信息的Excel表格。作者面临的问题是数据量大,运行时间长,且序列号在两个源文件中顺序不一致,存在部分序列号在任一文件中都不存在的情况。提供的解决方案是一个修改后的Python脚本,通过`pd.merge()`函数进行外连接合并,并将结果保存到新的Excel文件。该脚本成功解决了问题。
摘要由CSDN通过智能技术生成

I need to create an excel sheet comparing two sample sheets one contains the serial number and other information. Second sheet contains the warranty date. For example,

source1 sheet contains data as below

Model Serial Location

Dell 1234 A

Thoshiba 2345 B

Apple 3456 C

Cisco 4567 D

Sun 5678 E

source2 contains data as below

Serial Warranty Status

2345 1/1/2010

4567 2/2/2012

1112 3/2/2015

and the result should be

Model Serial Location Warranty Status

Dell 1234 A Not Found

Thoshiba 2345 B 1/1/2010

Apple 3456 C Not Found

Cisco 4567 D 2/2/2012

Sun 5678 E Not Found

Not Found 1112 Not Found 3/2/2015

I have found some sample scripts but my scenario contains:

Large no of data, it takes so much time to run

Serial number doesn't comes in the same order in both source1 and source2 files

Cases are there in which serial number doeskin exist in either of the source file

Please give me some suggestions and best algorithm to do this faster.

解决方案

try the below code, which I modified :

import pandas as pd

source1_df = pd.read_excel('a.xlsx', sheetname='source1')

source2_df = pd.read_excel('a.xlsx', sheetname='source2')

joined_df = pd.merge(source1_df,source2_df,on='Serial',how='outer')

joined_df.to_excel('/home/user1/test/result.xlsx')

I'm not an expert in python but above one worked.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值