I set up a summary dataframe to only show the high/low of 3 measures across the country and then color coded the negative values. I'm stuck on also bolding the negative values in the same step. When I try:
color = '#AF0061' & 'font-weight: bold'
It says unsupported operand type(s) for &: 'str' and 'str', but I don't get why. I thought I was only applying the function to the numeric columns.
#define min/max index (State) and value for each metric
mx_Rs =df.loc[df.DeltaReadmitRate == df.DeltaReadmitRate.max(), 'State'].values[0]
mn_Rs =df.loc[df.DeltaReadmitRate == df.DeltaReadmitRate.min(), 'State'].values[0]
mx_R = df['DeltaReadmitRate'].max()/100
mn_R = df['DeltaReadmitRate'].min()/100
mx_Ts =df.loc[df.DeltaTAT == df.DeltaTAT.max(), 'State'].values[0]
mn_Ts =df.loc[df.DeltaTAT == df.DeltaTAT.min(), 'State'].values[0]
mx_T = df['DeltaTAT'].max()/100
mn_T = df['DeltaTAT'].min()/100
mx_Ls =df.loc[df.DeltaLOS == df.DeltaLOS.max(), 'State'].values[0]
mn_Ls =df.loc[df.DeltaLOS == df.DeltaLOS.min(), 'State'].values[0]
mx_L = df['DeltaLOS'].max()/100
mn_L = df['DeltaLOS'].min()/100
#convert to dataframe
data = {'State': [mx_Rs, mn_Rs],
'Readmission Rate': [mx_R, mn_R],
'State ': [mx_Ts, mn_Ts],
'Turnaround Time': [mx_T, mn_T],
'State ': [mx_Ls, mn_Ls],
'Length of Stay': [mx_L, mn_L],
}
df_Summary = pd.DataFrame(data)
#insert new column "Delta and plug in values"
df_Summary.insert(0, "Delta", ['Worst Increase', 'Biggest Improvement'], True)
#format
numeric_columns = ['Readmission Rate', 'Turnaround Time', 'Length of Stay']
col_format = {'Readmission Rate': '{:,.2%}', 'Turnaround Time':'{:,.2%}', 'Length of Stay':'{:,.2%}'}
def format_font(x):
if x < 0:
color = '#AF0061'
else:
color = 'black'
return f'color: {color}'
df_styled = df_Summary.style.\
format(col_format).\
applymap(format_font, subset=numeric_columns).\
hide(axis='index').\
set_table_styles(
[{'selector': 'th',
'props': [('background', '#78BE20'), ('color', 'white'), ('font-weight', 'bold'), ('text-align', 'right')]},
{'selector': 'th.row_heading',
'props': [('background', 'white'), ('color', 'black'),('font-weight', 'bold'), ('text-align', 'center')]},
]
)