Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the .head() method of a pandas styler object?

I am working with a very big data set (18000 rows of data) which I only want to show a couple of rows such as 5 or 10 first rows. I was trying to use pandas.DataFrame().head(10) method but I am doing some styling and formatting and I receive the following error:

AttributeError: 'Styler' object has no attribute 'head'

This is how the styling looks like:

df.style.set_table_styles([{'props': [('font-size', '9pt'), 
                                            ('line-height', '100%')]}])

What is the best solution to this?

like image 714
Arad Haselirad Avatar asked Oct 27 '25 10:10

Arad Haselirad


2 Answers

I actually figured I could use the .head() method before assigning the styling to the data frame as follows:

df_ = df.head(10).style.set_table_styles([{'props': [('font-size', '9pt'), 
                                            ('line-height', '100%')]}])
df_

This worked quit well. Thank you for the comments.

like image 137
Arad Haselirad Avatar answered Oct 29 '25 00:10

Arad Haselirad


If you have a df that is already styled (of type pandas.io.formats.style.Styler), you can export the style, select head (using .data to access pandas.core.frame.DataFrame object), and then reuse the style:

style = df.export()
df.data.head(10).style.use(style)
like image 20
m7s Avatar answered Oct 28 '25 22:10

m7s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!