How to color code the column in DataFrame by comparing with other column

Viewed 18

I am reading excel into DataFrame, comparing the columns and color code the column for the value that does not match and writing to excel. Below is my code and it's not working. Anyone help me on this?

file = Path(path to excel)
df = pd.read_excel(file)

def highlight(row):
     if row['AMOUNT A'] != row['AMOUNT B'] and row['AMOUNT C'] != row['AMOUNT D']:
          color = 'red'
     background = ['background-color: {}'.format(color) for _ in row]
     return background
df.style.apply(highlight, subset=['IND_DB_C_INN'])

writer = pd.ExcelWriter(path to excel)
df.to_excel(writer, 'data')
writer.save()

df

AMOUNT A    AMOUNT B    AMOUNT C    AMOUNT D
1400            1400        
3000            3000        
1500            2500       2400       2300
3500            3500        

Output I want to see in excel:

enter image description here

0 Answers
Related