简体   繁体   中英

Pandas to fill the color to the column header or Entire dataframe

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:

在此处输入图片说明

OR

Fill the color to the whole background

在此处输入图片说明

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM