'How to color cells within a column based on Cell Value?

I'm trying to color cells red within specified columns of a large XLSX file depending on if the Value in column U is "N".

I'm able to achieve this with my code below but it's taking over 20 minutes, would anyone have any suggestions on how I can improve performance to make this run faster?

import pandas as pd
import time

startTime = time.time()

filePath = r"C:\\Users\\Desktop\\Match\\"
parameters = 'large_file.xlsx'

df = pd.read_excel(filePath + parameters)

cusip_match = 'N'

#color columns U, Units & units.1 based on if there is a "N" Cell in column U.
colorMatch = df.style\
    .apply(lambda x: ['background-color: red' if x == cusip_match else x for x in df.U],subset=['U'])\
    .apply(lambda x: ['background-color: red' if x == cusip_match else x for x in df.U],subset=['Units'])\
    .apply(lambda x: ['background-color: red' if x == cusip_match else x for x in df.U],subset=['Units.1'])

#output File
colorMatch.to_excel(r"C:\\Users\\Desktop\\Match\\Large_Compare_Matches.xlsx")

executionTime = (time.time() - startTime)
print('Execution time in seconds: ' + str(executionTime))


Solution 1:[1]

DataFrame(df['U']).style.apply(lambda x: 'background-color: red' if x == cusip_match else x)

Sources

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

Source: Stack Overflow

Solution Source
Solution 1