Python Forum
How to highlight dataframe columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to highlight dataframe columns
#1
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'
Reply
#2
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/st...Frame.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).
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add columns to polars dataframe sayyedkamran 1 1,756 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  concat 3 columns of dataframe to one column flash77 2 836 Oct-03-2023, 09:29 PM
Last Post: flash77
  Convert several columns to int in dataframe Krayna 2 2,391 May-21-2021, 08:55 AM
Last Post: Krayna
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 3,637 Jan-27-2021, 10:59 AM
Last Post: epsilon
  Adapting a dataframe to the some of columns flyway 2 2,046 Aug-12-2020, 07:21 AM
Last Post: flyway
  Difference of two columns in Pandas dataframe zinho 2 3,358 Jun-17-2020, 03:36 PM
Last Post: zinho
  DataFrame: To print a column value which is not null out of 5 columns mani 2 2,103 Mar-18-2020, 06:07 AM
Last Post: mani
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 2,456 Mar-14-2020, 09:19 AM
Last Post: scidam
  Interate for loop over certain columns in dataframe Finpyth 2 1,940 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  Does the order of columns in the DataFrame matter? new_to_python 11 5,981 Feb-17-2020, 12:32 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020