2

I would like to color a column in my dataframe (using the method proposed in a given answer, link below). So (taking only 1st row of my dataframe) I used to change the color the following code expression:

data1.head(1).style.set_properties(**{'background-color': 'red'}, subset=['column10'])

However it causes another problem: it changes the format of my dataframe (= addes more 0's after decimal point..). Is there any possibility I can keep the old format of my dataframe and still be able to color a column? Thanks in advance

Old output (first row):

2021-01-02  32072   0.0 1831    1831    1.0 1   0   1   0.0 1.0 

New output (first row):

2021-01-02  32072   0.000000    1831    1831    1.000000    1   0   1   0.000000    1.000000    1.000000    

Colouring one column of pandas dataframe

Champion
  • 93
  • 6
  • check my edit, I misread and first made it for columns. The edit is how you can apply a color to 1 column and not change the format for all the columns – Michael S. Aug 01 '22 at 17:29

1 Answers1

2

~~ Edit, as I see you wanted rows not columns:

Once you apply the style code, it changes the pandas dataframe from a pandas.core.frame.DataFrame object to a pandas.io.formats.style.Styler object. The styler object treats floats differently than the pandas dataframe object and yields what you see in your code (more decimal points). You can change the format with style.format to get the results you want:

data = [{"col1":"2021-01-02", "col2":32072,   "col3":0.0, "col4":1831,    "col5":1831,    
           "col6":1.0, "col7":1,   "col8":0,   "col9":1,   "column10":0.0, "col11":1.0}] 
data1 = pd.DataFrame(data)
data1 = data1.style.format(precision=1, subset=list(data1.columns)).set_properties(**{'background-color': 'red'}, subset=['column10'])
data1

Output:

enter image description here

Once you use style, it is no longer a pandas dataframe and is now a Styler object. So, normal commands that work on pandas dataframes no longer work on your newly styled dataframe (e.g. just doing head(10) no longer works). But, there are work arounds. If you want to look at only the first 10 rows of your Styler after you applied the style, you can export the style that was used and then reapply it to just look at the top 10 rows:

data = [{"col1":"2021-01-02", "col2":32072,   "col3":0.0, "col4":1831,    "col5":1831,    
           "col6":1.0, "col7":1,   "col8":0,   "col9":1,   "column10":0.0, "col11":1.0}] 
data1 = pd.DataFrame(data)
data1 = data1.append(data*20).reset_index(drop=True)
data1 = data1.style.format(precision=1, subset=list(data1.columns)).set_properties(**{'background-color': 'red'}, subset=['column10'])

Gives a large dataframe:

enter image description here

And then using this code afterwards will head (ie show) only 10 of the rows:

style = data1.export()
data1.data.head(10).style.use(style).format(precision=1, subset=list(data1.columns))

Now it is only showing the first 10 rows: enter image description here

Michael S.
  • 3,050
  • 4
  • 19
  • 34
  • Many thanks for your reply, very much appreciated. I tried to do this: data1.head(1) = data1.style.format(precision=1, subset=list(data1.columns)).set_properties(**{'background-color': 'red'}, subset=['perfLO']) data1.head(1) But I have error message: SyntaxError: cannot assign to function call – Champion Aug 01 '22 at 19:02
  • What does this comment mean? Are you having problems? – Michael S. Aug 01 '22 at 19:09
  • PS Actually, what you propose, it works)!!! The dataframe data1 with a selected column is created!!.. But then, I only want to look at the data1.head (10)... and I have the following error. Any workaround possible?.--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) C:\Users\OGLUZD~1\AppData\Local\Temp/ipykernel_20512/3753329706.py in ----> 1 data1.head(10) AttributeError: 'Styler' object has no attribute 'head' – Champion Aug 01 '22 at 19:10
  • So I do (and then I only want to get the data1.head(10)..). Thanks!!: data1 = data1.style.format(precision=1, subset=list(data1.columns)).set_properties(**{'background-color': 'red'}, subset=['perfLO']) – Champion Aug 01 '22 at 19:13
  • What you have to realize is that your dataframe is no longer a pandas dataframe after you use the style function. So, code that works on a pandas dataframe will no longer work on your styled dataframe (you can apply code to the Styler dataframe, but it is different than the pandas dataframe). Check the edit for how to see just the first 10 rows – Michael S. Aug 01 '22 at 19:32
  • Great reply, thank you very much/clear now!! Is there a way to have the same number of digits after the comma as in original dataframe, in case/not to use "precison" parameter as it formats everything to 1 or 2 numbers after the comma? Thanks in advance – Champion Aug 01 '22 at 20:41