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

Viewed 323

I have two workbooks that needs to be compared and then print the differences into a 3. workbook. There is only one worksheet in each. for now, I user pandas to compare and print the result into the command line. I'm very new to Python and do not know much yet.

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)
1 Answers

Your goal is not to print the difference, but to save the difference to an Excel file. For that, one might use pandas.DataFrame.to_excel.

Considering one wants to save the row names, one needs to pass index=True

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