I have a pandas dataframe:
import pandas as pd
import numpy as np
df = pd.DataFrame({'foo':[1,2, 3, 4],
'bar':[[1,2,0.04], [1,2,0.04], [1,2,0.06], np.nan]})
display(df)
def stars(x, sign_level):
if x is np.nan:
return ''
else:
p_value = x[2]
if p_value < sign_level:
return '*'
else:
return ''
df['marker'] = df.bar.apply(stars, sign_level=0.05)
df
Instead of adding a column with a star in case the result is considered significant, is it possible to format the cell (like in an Excel sheet) as bold?
display DataFrame() values in bold font in one row only seems to be able to format a whole row - I would like to reformat only a specific cell
Conditionally format Python pandas cell
seems similar, though they only change the background, not format as bold.
edit
the code below can already change the background color - I just do not know how to format as bold.
def highlight_significant(x, sign_level):
if x is np.nan:
return ''
else:
if isinstance(x, list):
p_value = x[2]
color = 'lightgreen' if p_value < sign_level else ''
if p_value < sign_level:
return 'background-color: %s' % color
else:
return ''
else:
return ''
df.style.applymap(highlight_significant, sign_level=0.05)