11

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.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Leockl
  • 1,906
  • 5
  • 18
  • 51
  • https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.io.formats.style.Styler.html – Thomas Apr 22 '20 at 11:15
  • Thanks @PNX, but I can't seem to see any references to my question about printing a pandas.io.formats.style.Styler object – Leockl Apr 22 '20 at 11:58

3 Answers3

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)
Leockl
  • 1,906
  • 5
  • 18
  • 51
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.

mtchaves
  • 49
  • 4
  • 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)? – Leockl Apr 22 '20 at 11:56
  • 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. (https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html) – mtchaves Apr 22 '20 at 15:10
  • Thanks @mtchaves. I have actually found a way to do this. See my answer below if interested. – Leockl Apr 22 '20 at 22:31
  • 1
    @mtchaves - 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? – Cerberton May 22 '21 at 14:48
0

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)

with open('str.html','w') as f:
    df.to_html(f)

filename = ' str.html'
webbrowser.open_new_tab(filename)

ניר
  • 1,204
  • 1
  • 8
  • 28