Python Forum
How to highlight dataframe columns - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to highlight dataframe columns (/thread-24706.html)



How to highlight dataframe columns - SriRajesh - Feb-28-2020

Hi,
I have below dataframe, I want to highlight P with green, F with red.
df:
  c1 c2 c3
A P  na P  
B F  F  P
C P  P  na
and write to excel.
I use below code,

df.conditional_format('B2:D4', {'type': '2_color_scale'})
    
df.to_excel("output.xlsx", sheet_name=sheet1)


giving below error:
AttributeError: 'DataFrame' object has no attribute 'conditional_format'


RE: How to highlight dataframe columns - Marbelous - Feb-28-2020

Pandas has some amazing methods to help you plot and present its data but I don't think it has anything specific to the way fonts are displayed. Here is the doc page on the dataframe:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html
There are no formatting options for color even in the style attribute which is for HTML/CSS.

You could do this right in Excel with conditional formatting or I guess do it programmatically with one of the python excel modules (I've never tried to use them for that).