0

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')]},
]
)

enter image description here

BP12
  • 27
  • 6

2 Answers2

2

& is the binary addition operator. You might have meant + as @DonPre suggests, but that won't quite work either, as the properties should need to be separated by ;.

I'm pretty sure the following works but don't have a df handy to test it against

def format_font(x):
    if x < 0:
        style = "color: #AF0061; font-weight: bold"
    else:
        style = "color: black"
    
    return style

nigh_anxiety
  • 1,428
  • 2
  • 4
  • 12
  • It worked! Thank you so much. Any advice on cleaning up my sloppy manual approach to getting min/max of each measure and the corresponding index, e.g. State? I can get the values, but I can't seem to get the state: df_blah = df[['DeltaReadmitRate', 'DeltaLOS','DeltaTAT']].agg(['min', 'max']) – BP12 Oct 20 '22 at 15:56
0

I think that the problem is that you are trying to concatenate 2 string '#AF0061' and 'font-weight: bold'.

If you want to do that you need to use the + operator.

If you need more information on concatenating string you can chek this post

DonPre
  • 158
  • 7