2016-07-11 40 views
0

我需要创建一个excel表,比较两个样本表,其中一个包含序列号和其他信息。第二张表包含保修日期。例如, 来源1页包含的数据如下python比较两个excel表并追加正确的记录

Model  Serial  Location 
Dell  1234  A 
Thoshiba 2345  B 
Apple  3456  C 
Cisco  4567  D 
Sun   5678  E 

源2包含数据如下

Serial Warranty Status 
2345 1/1/2010 
4567 2/2/2012 
1112 3/2/2015 

,其结果应该是

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 

我已经发现了一些示例脚本,但我的情况包含:

  1. 大数据量,需要很长时间才能运行
  2. 序列号在source1和source2文件中的顺序并不相同
  3. 有一些情况存在序列号存在于源文件

请给我一些建议和最佳算法来做到这一点更快。

回答

1

尝试下面的代码,这是我修改:

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') 

我不是python专家,但超过一个工作。

0

pandas安装,那么可以将每个片加载作为数据帧和由Serial加入:

import pandas as pd 

source1_df = pd.read_excel('path/to/excel', sheetname='source1_sheet_name') 
source2_df = pd.read_excel('path/to/excel', sheetname='source2_sheet_name') 

joined_df = source1_df.join(source2_df, on='Serial') 

joined_df.to_excel('path/to/output_excel') 
+0

为此得到一些错误。 http://stackoverflow.com/questions/38347985/python-pandas-merging-excel-sheets-not-working – theG