1

I am trying to understand how to fill the color specific to dataframe column header or to the entire dataframe background.

I've just started and google around but did not get understanding to to it, there are good links such as related link and another Link .

Datatframe Below..

>>> df = pd.DataFrame({ 'Name':['James', 'Ricardo', 'Arie', 'Ruben', 'Naveen', 'Audery'], 'Sir Name':['Pullman', 'Jacob', 'Tilman', 'Juneja', 'Therry', 'Cuenin'], 'Country':['Holland', 'Holland', 'Holland', 'Holland', 'Holland', 'Holland'], 'Age':[38,28,27,29,26,27] })

>>> df
      Name Sir Name  Country  Age
0    James  Pullman  Holland   38
1  Ricardo    Jacob  Holland   28
2     Arie   Tilman  Holland   27
3    Ruben   Juneja  Holland   29
4   Naveen   Therry  Holland   26
5   Audery   Cuenin  Holland   27

Expected:

enter image description here

OR

Fill the color to the whole background

enter image description here

I have tried as follows but did not get the result.

>>> ss = df.style.set_table_styles([{ 'selector': 'th', 'props': [('background-color', 'black'), ('color', 'cyan')]}])
>>> print(ss)
<pandas.io.formats.style.Styler object at 0x7f5eccfedac8>
user2023
  • 452
  • 5
  • 22
  • Check [this](https://stackoverflow.com/questions/50664839/format-excel-column-header-for-better-visibility-and-color) thread. – Erfan Mar 15 '20 at 10:48
  • Thanks @Erfan, but that links seems in particular to Excelwriter not to Pandas as dataframe header, may be i am not able to get that. – user2023 Mar 15 '20 at 10:55
  • When you're reading in excel, pandas uses excelwriter under the hood, you can use that engine to edit pandas dataframe headers – Erfan Mar 15 '20 at 10:58
  • but i'm not reading from the Excel. – user2023 Mar 15 '20 at 11:08
  • Is there a way to Fill the bgcolor while doing `df.to_html` on the Linux console as i am not using the Jupitor not book. – user2023 Mar 16 '20 at 04:29

1 Answers1

1

Style create html, from the doc:

If using in the Jupyter notebook, Styler has defined a _repr_html_ to automatically render itself. Otherwise call Styler.render to get the generated HTML.

That's why in a normal console the print only shows an object not the dataframe with colors.

You should try to use your style in a jupyter notebook.

ndclt
  • 2,590
  • 2
  • 12
  • 26
  • Thank ndclt for the answer , but i am curious to know how to achieve that in Linux Console i'm not using Jupitor. – user2023 Mar 15 '20 at 12:12