python 比较两个 excel sheet 并附加正确的记录
python compare two excel sheet and append correct record
我需要创建一个 excel sheet 比较两个样本 sheet,其中一个包含序列号和其他信息。第二个 sheet 包含保修日期。例如,
source1 sheet 包含如下数据
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 文件中的顺序不同
- 存在序列号存在于任一源文件中的情况
请给我一些建议和最佳算法以更快地完成此操作。
安装 pandas
,然后您可以将每个 sheet 作为数据帧加载并通过 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')
试试下面我修改过的代码:
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 方面的专家,但上面的一个行得通。
我需要创建一个 excel sheet 比较两个样本 sheet,其中一个包含序列号和其他信息。第二个 sheet 包含保修日期。例如, source1 sheet 包含如下数据
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 文件中的顺序不同
- 存在序列号存在于任一源文件中的情况
请给我一些建议和最佳算法以更快地完成此操作。
安装 pandas
,然后您可以将每个 sheet 作为数据帧加载并通过 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')
试试下面我修改过的代码:
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 方面的专家,但上面的一个行得通。