You can check for differences by comparing each element of each corresponding row (here I use the unique id
column to find corresponding rows). If there is a difference you can append it to a new dataframe. Finally save the new dataframe to excel format.
df_differnt_rows = pd.DataFrame(columns=['id','B','C'])
df1 = pd.DataFrame([[1,2,3],[2,2,3],[3,2,3]], columns=['id','B','C'])
df2 = pd.DataFrame([[1,2,3],[2,"different",2],[3,2,3]], columns=['id','B','C'])
for i, row in df1.iterrows():
compare_row = df2.loc[df2['id'] == row['id']].iloc[0]
if all(row == compare_row):
continue
df_differnt_rows = df_differnt_rows.append(compare_row)
This produces another df that has all the rows that are different between df1 and df2.
print(df_differnt_rows)
id B C
1 2 different 2
Save using .to_excel()
method:
df_differnt_rows.to_excel('df_differnt_rows.xlsx')
Check out openpyxl (i.e. patternfill) if you want to highlight cells in the excel file.