I already had a question answered for conditionally applying color with a normal Dataframe. Link
The question I have is how do I do that with a multi-index data-frame? The dataframe in the previous question was
Value Limit Actual
Issues < 33 0
Rating > 4 4.2
Complaints < 15 18
Time 30 - 45 41
Tip -- -
The new dataframe is the same dataframe but with header like below
df.columns =pd.MultiIndex.from_product([['summary'], df.columns])
df
So the new dataframe is like so
Summary
Value Limit Actual
Issues < 33 0
Rating > 4 4.2
Complaints < 15 18
Time 30 - 45 41
Tip -- -
The out put expected is like the previous question itself but with a additional header row at the top.
I tried replacing row['Limit'] with x.loc[:,idx[:,'Limit']] where idx is pd.IndexSlice but it did not work
import re
def highlight(row):
numbers = re.findall('[0-9]+', row['Limit'])
if row['Value'] in ('Issues','Rating','Complaints'):
if '>' in row['Limit'] and row['Actual'] > numbers[0]:
color = 'green'
elif row['Actual'] < numbers[0]:
color = 'green'
else:
color = 'red'
else:
if len(numbers) == 0:
color = 'yellow'
elif row['Actual'] > numbers[0] and row['Actual'] < numbers[1]:
color = 'green'
else:
color = 'red'
return f"background-color: {color}"
