Open In App

Pandas.set_option() function in Python

Last Updated : 28 Jul, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Pandas have an options system that lets you customize some aspects of its behavior, display-related options being those the user is most likely to adjust. Let us see how to set the value of a specified option.

set_option()

Syntax : pandas.set_option(pat, value) Parameters :
  • pat : Regexp which should match a single option.
  • value : New value of option.
Returns : None Raises : OptionError if no such option exists
Example 1 : Changing the number of rows to be displayed using display.max_rows. Python3 1==
# importing the module
import pandas as pd

# creating the DataFrame
data = {"Number" : [0, 1, 2, 3, 4, 
                    5, 6, 7, 8, 9],
        "Alphabet" : ['A', 'B', 'C', 'D', 'E', 
                      'F', 'G', 'H', 'I', 'J']}
df = pd.DataFrame(data)

print("Initial max_rows value : " + 
      str(pd.options.display.max_rows))

# displaying the DataFrame
display(df)

# changing the max_rows value
pd.set_option("display.max_rows", 5)

print("max_rows value after the change : " + 
      str(pd.options.display.max_rows))

# displaying the DataFrame
display(df)
Output : Example 2 : Changing the number of columns to be displayed using display.max_columns. Python3 1==
# importing the module
import pandas as pd

# creating the DataFrame
data = {"Number" : 1,
        "Name" : ["ABC"],
        "Subject" : ["Computer"],
        "Field" : ["BDA"],
        "Marks" : 70}
df = pd.DataFrame(data)

print("Initial max_columns value : " + 
      str(pd.options.display.max_columns))

# displaying the DataFrame
display(df)

# changing the max_columns value
pd.set_option("display.max_columns", 3)

print("max_columns value after the change : " + 
      str(pd.options.display.max_columns))

# displaying the DataFrame
display(df)
Output :

Next Article
Article Tags :
Practice Tags :

Similar Reads