0

I have noticed that when we set some options for pandas DataFrames such as pandas.DataFrame('max_rows',10) it works perfectly for DataFrame objects.

However, it has no effect on Style objects.

Check the following code :

import pandas as pd
import numpy as np

data= np.zeros((10,20))

pd.set_option('max_rows',4)
pd.set_option('max_columns',10)
df=pd.DataFrame(data)

display(df) 
display(df.style)

Which will result in : Result

I do not know how to set the properties for Style object.

Thanks.

2 Answers2

2

Styler is developing its own options. The current version 1.3.0 of pandas has not got many. Perhaps only the styler.render.max_elements.

Some recent pull requests to the github repo are adding these features but they will be Stylers own version.

Attack68
  • 4,437
  • 1
  • 20
  • 40
  • I see, First of all, thank you so so much, I have been trying to figure this out for the whole day. I think `styler.render.max_elements` is better than nothing. However, I am trying to change write a function to change the html code that basically does the same thing as max_row and max_column. Not sure if I'll be successfully. @Attack68 – Mohammad Badri Ahmadi Aug 27 '21 at 17:45
  • hey @Attack69, I found a way to mimic set_option(max_row) and set_option(max_columns) for styler objects. Though might be of your interest. – Mohammad Badri Ahmadi Aug 28 '21 at 05:46
1

As @attack69 mentioned, styler has its own options under development. However, I could mimic set_option(max_row) and set_option(max_columns) for styler objects. Check the following code:

import pandas as pd
import numpy as np

data= np.zeros((10,20))
mx_rw=4
mx_cl=10
pd.set_option('max_rows',mx_rw)
pd.set_option('max_columns',mx_cl)
df=pd.DataFrame(data)

display(df) 
print(type(df))


df.loc[mx_rw/2]='...'
df.loc[:][mx_cl/2]='...'

temp=list(range(0,int(mx_rw/2),1))
temp.append('...')
temp.extend(range(int(mx_rw/2)+1,data.shape[0],1))
df.index=temp
del temp

temp=list(range(0,int(mx_cl/2),1))
temp.append('...')
temp.extend(range(int(mx_cl/2)+1,data.shape[1],1))
df.columns=temp
del temp

df=df.drop(list(range(int(mx_rw/2)+1,data.shape[0]-int(mx_rw/2),1)),0)
df=df.drop(list(range(int(mx_cl/2)+1,data.shape[1]-int(mx_cl/2),1)),1)
df=df.style.format(precision=1)

display(df)
print(type(df))

which both DataFrame and Styler object display the same thing. Result

  • 1
    this will prevent global styler methods such as `highlight_max` over a column from working. But the render trimming in development for pandas 1.4.0 will allow trimmed dataframes but with still valid styling – Attack68 Aug 28 '21 at 13:37
  • That is very true, however, I am using what you mentioned in the other [question](https://stackoverflow.com/questions/68880224/how-to-make-pandas-dataframe-python-to-display-each-cell-in-a-two-dimensional/68896830#68896830) to modify each cell using HTML code basically. even for a simple data. The main problem is, it is not very efficient and takes more time. I just like the freedom of HTML coding. – Mohammad Badri Ahmadi Aug 28 '21 at 15:09