How to render pandas data frame as publication ready table while highlighting maximum value in column

Viewed 122

I am able to highlight maximum value of pandas columns using "apply(highlight_max)", I can also render tables using matplotlib "ax.table" to make nice looking tables. However, how can I easily convert pandas table with maximum values highlighted and as a nice looking table?

1 Answers

Actually, I had the same problem a few months ago. I could convert df to latex table using:

df.to_latex()

I don't know if you can make it highlighted in latex format or not but I know it is possible to highlight the selected item with this piece of code:

def highlight_(s):
    to_highlight = (s > index_max) & (s > column_max.loc[s.name])
    return ['background-color: yellow' if v else '' for v in to_highlight]

df.colname.apply(highlight_)
Related