python比较两个excel表单并追加正确的logging
我需要创build一个Excel表,比较两个样本表,一个包含序列号和其他信息。 第二张表包含保修date。 例如,source1表包含如下的数据
Model Serial Location Dell 1234 A Thoshiba 2345 B Apple 3456 C Cisco 4567 D Sun 5678 E
source2包含如下的数据
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
我发现了一些示例脚本,但是我的场景包含:
- 数据量大,需要很长时间才能运行
- 序列号在source1和source2文件中的顺序不一样
- 在源文件中存在序列号存在的情况
请给我一些build议和最好的algorithm,以更快地做到这一点。
尝试下面的代码,我修改:
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的专家,但上面的工作。
安装pandas
,然后你可以加载每个表作为一个数据框,并通过Serial
join:
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')