Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a pandas.io.formats.style.Styler object

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.

like image 527
Leockl Avatar asked Apr 22 '20 11:04

Leockl


People also ask

What is a styler object Python?

Style property returns a styler object which provides many options for formatting and displaying dataframes. A styler object is basically a dataframe with some style. In this article, we will go through 10 examples to master how styling works.

What is Format () in pandas?

Introduction to Pandas format. Pandas format is indicating the information in the necessary organization. Some of the time, the worth is huge to the point that we need to show just wanted aspect of this or we can say in some ideal configuration. Python pandas library utilizes an open-source standard date-time design.

How do I beautify a Dataframe in Python?

You can pretty print pandas dataframe using pd. set_option('display. max_columns', None) statement. Usecase: Your dataframe may contain many columns and when you print it normally, you'll only see few columns.


2 Answers

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)
like image 105
Leockl Avatar answered Nov 14 '22 21:11

Leockl


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.

like image 33
mtchaves Avatar answered Nov 14 '22 23:11

mtchaves