Mohammad Badri Ahmadi
Mohammad Badri Ahmadi

Reputation: 68

how to apply Pandas.set_option (Python) to pandas.style objects

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 Styler object.

Upvotes: 1

Views: 880

Answers (2)

Mohammad Badri Ahmadi
Mohammad Badri Ahmadi

Reputation: 68

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

Upvotes: 1

Attack68
Attack68

Reputation: 4785

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.

Upvotes: 2

Related Questions