'Compare Entire Row of two excel files only when first column matches

I want to compare two excel files, which I have sorted how to do cell by cell. However, I want to compare an entire row only when the Ax_file1 = Ax_file2, then spit out a file that shows how it changed. IE the way I have done this works to find differences only if the row count is the same, I need it to be more robust.

Here is my code as it stands:

import pandas as pd
import numpy as np
from tkinter import *
import os
import tkinter.font as font
#

def call1():
    dfpri = pd.read_excel('EnetBefore.xlsx')

    replacement_mapping_dict = {
        ' -N-       NONE': ' -N-       BN16',
        ' -N-       OPER': ' -N-       BN08',
        ' -N-       SMOKE': ' -N-       BN10',
        ' -N-       EMER': ' -N-       BN12',
        ' -N-       PDL': ' -N-       BN14',
        ' *F*       NONE': ' *F*       BN16',
        ' *F*       OPER': ' *F*       BN08',
        ' *F*       SMOKE': ' *F*       BN10',
        ' *F*       EMER': ' *F*       BN12',
        ' *F*       PDL': ' *F*       BN14'
    }
    # dfpri2 = dfpri.replace(replacement_mapping_dict)
    # TESTING CODE
    # this was just to test
    #dfpri2.to_excel('temp001.xlsx', index=False,header=True)
    #dfbef = pd.read_excel('temp001.xlsx')
    dfbef = dfpri.replace(replacement_mapping_dict)
    dfaft = pd.read_excel('BacnetAfter.xlsx')


    comparevalues = dfbef.values == dfaft.values
    # TESTING CODE
    #print(comparevalues)

    rows,cols = np.where(comparevalues==False)

    for item in zip(rows,cols):
        dfbef.iloc[item[0],item[1]] = ' {} ==> {} '.format(dfbef.iloc[item[0], item[1]], dfaft.iloc[item[0],item[1]])
    

    dfbef.to_excel('output_temp.xlsx', index=False,header=True)
    #dffinal = pd.read_excel('output_temp99.xlsx')

def make_your_style(val):
    #"""
    #Takes a scalar and returns a string with
    #the css property `'background-color: red'` for string with -->
    #"""
    color = 'red' if '==>' in str(val) else 'white'
    #color = 'white' if 'nan' in str(val) else ''
    return f'background-color: {color}'

def call2(name):
    dffinal1 = pd.read_excel('output_temp.xlsx')
    dffinal1.drop(['B', 'C', 'D', 'F'], axis=1, inplace=True)
    dffinal2 = dffinal1.style.applymap(make_your_style)

    dffinal2.to_excel(name, engine = 'openpyxl',index=False)

root = Tk()
root.title("Compare Pre and Post PPLR")
#root.geometry("500x200")
entry = Entry(root, width=100, bd=5,) 
entry.insert(0, "Enter name of output file here with no extension")
entry.pack()

def clearText():
   entry.delete(0, END)


def myClick():
    call1()
    name2 = str(entry.get())+".xlsx"
    call2(name2)
    myLabel = Label(root, text="Check output folder"+" for: "+ name2)
    myLabel.pack()
    
    
# no () after func call in command
myFont2 = font.Font(size=20)
myFont = font.Font(size=30)
myButton2 = Button(root, text="Clear", command=clearText, fg="black", bg="grey", padx=80,pady=20)
myButton2['font'] = myFont2
myButton2.pack()
myButton = Button(root, text="Click to Compare", command=myClick, fg="black", bg="green", padx=200,pady=100)
myButton['font'] = myFont
myButton.pack()
# pack

root.mainloop()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source