简体   繁体   English

在python中比较两个.xlsx工作簿并将差异打印到3.工作簿中

[英]Compare Two .xlsx workbooks and print differences into a 3. workbook in python

I have two workbooks that needs to be compared and then print the differences into a 3. workbook.我有两个需要比较的工作簿,然后将差异打印到 3. 工作簿中。 There is only one worksheet in each.每个工作表中只有一个工作表。 for now, I user pandas to compare and print the result into the command line.现在,我使用 Pandas 来比较结果并将结果打印到命令行中。 I'm very new to Python and do not know much yet.我对 Python 很陌生,还不太了解。

Here is the code I have so far:这是我到目前为止的代码:

import pandas as pd

df1 = pd.read_excel('DNS_Master_List.xlsx')
df2 = pd.read_excel('CopyList.xlsx')
difference = df1[df1!=df2]
print (difference)

Your goal is not to print the difference, but to save the difference to an Excel file.您的目标不是打印差异,而是将差异保存到 Excel 文件中。 For that, one might use pandas.DataFrame.to_excel .为此,可以使用pandas.DataFrame.to_excel

Considering one wants to save the row names, one needs to pass index=True考虑到要保存行名,需要传递index=True

difference.to_excel('file.xlsx', index=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM