I am trying to automate a few tasks on excel, some include setting the cells without any values in red color (empty cells in my DataFrame dimensions and not outside it), I tried the following after checking previous similar answers:
import pandas as pd
# Create a dataframe
df = pd.read_excel(r'input.xls', sheet_name='sheet1')
print(df)
df.style.applymap(lambda x: 'background-color : yellow' if x>1 else '')
# create excel writer object
writer = pd.ExcelWriter(r'Output.xls')
# write dataframe to excel
df.to_excel(writer)
# save the excel
writer.save()
print('DataFrame is written successfully to Excel File.')
I've also tried other ways like
def color(row):
if row.isnull().values.any() == True:
return ['background-color: red'] * len(row)
return [''] * len(row)
# Apply the function
df.style.apply(color, axis=1)
None of which seem to work, in the console I am getting the proper values printed and I am getting an output file with the additional row enumeration from 0, but nothing is getting colored in the output excel file
My dataset in excel has x by y dimensions and each cell can contain numbers(decimal) or text depending on the column name