I am facing an issue while applying an applymap() with lambda function to replace the existing string from Styler Dataframe. E.g. We have to replace Change with 'yellow' color and New with 'green'. I stuck in applying the function to replace value from Styler Dataframe.
import pandas as pd
out=r'C:\Users\test\changes.xlsx'
df = pd.read_excel(r'C:\Users\test\update.xlsx')
def mycolors(val):
#print(val)
stra = 'Changed-'
stri = 'New-'
color = 'white'
if stra in str(val):
color = 'yellow'
elif stri in str(val):
color = 'green'
#print(type(color))
return 'background-color: %s' % color
df = df.style.applymap(mycolors)
df = df.applymap(lambda x: str(x).replace('Changed-',''))
#print(df)
df.to_excel(out, header=True, index=False)