0
  1. I have two different excel for compare...

    enter image description here

  2. I want to highlight duplicated 'Name' and 'Reg Date' record in excels after merge.

  3. Am I use wrong function of 'pandas' or my logic incorrect?

    import pandas as pd 
    dt1 = pd.read_excel('Data1.xlsx')
    dt2 = pd.read_excel('Data2.xlsx')
    meg_rlt = pd.merge(dt1.iloc[:, [0, 1]], dt2.iloc[:, [0, 2]], on=['Name', 'Reg Date'], how='outer', indicator=True)
    for a in meg_rlt.iloc[:, [2]].values:
        if a == 'both':
           return meg_rlt.style.apply[background: green]
    
CHL
  • 87
  • 6
  • 1
    If you want Python answers then you may need to think about tagging Python. Also if you want Excel answers then tagging Excel is fine but if you don't want Excel based answers... – Solar Mike Jan 29 '22 at 11:15
  • Thanks your remind. – CHL Jan 29 '22 at 11:19

1 Answers1

0

After some research I found this post. This shows how to use apply to color rows and columns.
The solution below used the depicted approach (see function bg_color) and should fit your needs.
The resulting Excel-file highlights duplicate entries.

import pandas as pd

dt1 = pd.read_excel('Data1.xlsx')
dt2 = pd.read_excel('Data2.xlsx')

# Rename column to match data2.xlsx
dt1 = dt1.rename({"Student Name": "Name"}, axis='columns')

df_merge = pd.merge(dt1.iloc[:, [0, 1]], dt2.iloc[:, [0, 2]], on=[
    'Name', 'Reg Date'], how='outer', indicator=True)

i = 0
rows_to_color = []

for a in df_merge.iloc[:, [2]].values:
    if a == 'both':
        # remember the rows you want to color
        rows_to_color.append(i)
    i += 1


def bg_color(col):
    color = '#ffff00'
    # color every cell, where rows_to_color contains the index
    return ['background-color: %s' % color
            if i in rows_to_color
            else ''
            for i, x in col.iteritems()]  # i --> row; x --> value


styled = df_merge.style.apply(bg_color)

styled.to_excel("merged_file_styled.xlsx")
Na1k
  • 36
  • 5
  • 1
    Links are prone to "rot" (https://en.wikipedia.org/wiki/Link_rot) - ideally, solutions can persist (to contribute value to future users/members). Guidance is to ensure that whatever relevant info you reference by way of a link is available/reproduced within the reponse itself. I've always argued "what if I archived the link in Wayback Machine etc." - this doesn't seem to have been tolerated - despite my esaclation I never recieved a satisfactory reply to that challenge) - in any case, thanks for your contribution - it may be the case your code suffices - I'll let you judge.. – JB-007 Jan 29 '22 at 22:35
  • Thanks for your comment, truly didn't think about that. I updated my answer by removing one link and describing the what the other one contains. – Na1k Feb 01 '22 at 17:34
  • np mate - always nice to see new contributors so keep it up - the larger / wider field of solutions we can collate and keep organised/the further they reach and more of us/others supported/helped out from time to time. ta – JB-007 Feb 01 '22 at 20:53