How to print a pandas.io.formats.style.Styler object
Asked Answered
D

3

12

I have the following code which produces a pandas.io.formats.style.Styler object:

import pandas as pd
import numpy as np

df = pd.DataFrame({'text': ['foo foo', 'bar bar'],
                 'number': [1, 2]})

df1 = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
df2 = df1.set_properties(**{'text-align': 'center'}).hide_index()
df2   # df2 is a pandas.io.formats.style.Styler object

How do I print df2 if I have more code running underneath the above script, for eg.:

import pandas as pd
import numpy as np

df = pd.DataFrame({'text': ['foo foo', 'bar bar'],
                 'number': [1, 2]})

df1 = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
df2 = df1.set_properties(**{'text-align': 'center'}).hide_index()
df2

np.round(0.536, 2)

I tried using the print statement but it's giving me an output as below:

import pandas as pd
import numpy as np

df = pd.DataFrame({'text': ['foo foo', 'bar bar'],
                 'number': [1, 2]})

df1 = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
df2 = df1.set_properties(**{'text-align': 'center'}).hide_index()
print(df2)

np.round(0.536, 2)
<pandas.io.formats.style.Styler object at 0x000000000B4FAFC8>
0.54

Any help would really be appreciated. Many thanks in advance.

Dashpot answered 22/4, 2020 at 11:12 Comment(2)
pandas.pydata.org/pandas-docs/stable/reference/api/…Hrvatska
Thanks @PNX, but I can't seem to see any references to my question about printing a pandas.io.formats.style.Styler objectDashpot
D
14

I found the answer for this:

import pandas as pd
from IPython.display import display
import numpy as np

df = pd.DataFrame({'text': ['foo foo', 'bar bar'],
                 'number': [1, 2]})

df1 = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
df2 = df1.set_properties(**{'text-align': 'center'}).hide_index()
display(df2)

np.round(0.536, 2)
Dashpot answered 22/4, 2020 at 12:34 Comment(0)
P
4

The set_properties method is used to create a style applied to a dataframe. If you want to check the dataframe style after you change the properties you just need to print the dataframe you changed.

On your example you should do:

import pandas as pd

df = pd.DataFrame({'text': ['foo foo', 'bar bar'],
                 'number': [1, 2]})

df.style.set_properties(**{'text-align': 'center'})

print(df)

The method set_properties returns a Styler, not a dataframe. You can check the documentation here.

Pivoting answered 22/4, 2020 at 11:25 Comment(4)
Thanks @mtchaves. So there is no way for me to print the dataframe table with the styles that I have set (namely center-aligning the headers and values of the table and hiding the index)?Dashpot
Yes, when you print your df after applying a style it should appear differently. I recommend you to go through this document to understand it better. (pandas.pydata.org/pandas-docs/stable/user_guide/style.html)Pivoting
Thanks @mtchaves. I have actually found a way to do this. See my answer below if interested.Dashpot
@Pivoting - do you have any advice regarding number formatting? I am trying to print a dataframe to the terminal using df.describe.style.format('{:,}') but without success. The documentation isn't clear on how this could be achieved - perhaps I am missing something?Scotsman
M
1

This will show the df in your browser , styler also have the .to_html() method

import webbrowser
import pandas as pd

d = {
    "id":[22,23,24,25,26,27,28],
    "a":[1,2, 3, 4, 5, 6, 7],
    "b":[1,2, 3, 4, 5, 6, 7] 
}

df = pd.DataFrame(d)

filename = 'foo.html'
with open(filename,'w') as f:
    df.to_html(f)

webbrowser.open_new_tab(filename)

Mulhouse answered 27/1, 2022 at 11:15 Comment(2)
This is way better.Eleen
As of today, I would just use PyCharm or something like this though...Alkalimeter

© 2022 - 2025 — McMap. All rights reserved.