Reputation: 91
I am running for
loop in dataframe and I want to highlight the row in the output report whenever it matches certain conditions. However, my code is working for hardcoded value (i.e. 10), but I want to pass a variable. Any help appreciated.
df = pd.DataFrame({"ID": [10, 11, 12, 13, 14],
"NAME": ['item1', 'item2', 'item3', 'item4', 'item5']})
number_rows = len(df.index) + 1
writer = pd.ExcelWriter("Report.xlsx",engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
a = df['ID'][1]
format1 = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
worksheet.conditional_format("$A$1:$B$%d" % (number_rows),
{"type": "formula",
"criteria": '=INDIRECT("A"&ROW())= 10', # I want to pass variable (a) instead of '10'
"format": format1
}
)
workbook.close()
Upvotes: 0
Views: 635
Reputation: 91
Found a way to add variable using "Value" attribute
df = pd.DataFrame({"ID": [10, 11, 12, 13, 14],
"Status": ['item1', 'item2', 'item3', 'item4', 'item5']})
number_rows = len(df.index) + 1
writer = pd.ExcelWriter("Report.xlsx",engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
format1 = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
worksheet.conditional_format("$A$1:$B$%d" % (number_rows),
{"type": "cell",
'criteria': '=',
"value": a, #here a is variable now
"format": format1
}
)
workbook.close()
Upvotes: 1