
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Options and Customization
Pandas provides a API to customize various aspects of its behavior, particularly related to display settings. This customization is essential for adjusting how data is presented based on your needs. Whether you want to adjust how many rows and columns are displayed or change the precision of floating-point numbers, Pandas provides a flexible and powerful API for these customization's.
The primary functions available for these customization's are −
- get_option()
- set_option()
- reset_option()
- describe_option()
- option_context()
Frequently used Parameters
Before learning about the customization options, let's see about some of the frequently used Pandas display parameters that you can used for customization −
Sr.No | Parameter & Description |
---|---|
1 |
display.max_rows Maximum number of rows to display. |
2 |
2 display.max_columns Maximum number of columns to display. |
3 |
display.expand_frame_repr Whether to expand the display of DataFrames across multiple lines. |
4 |
display.max_colwidth Maximum width of columns. |
5 |
display.precision Precision to display for decimal numbers. |
Let us now understand how the customization's functions operate.
Getting the Current Options
The get_option() function retrieves the current value of a specified parameter. This is useful for checking the current configuration of Pandas.
Example: Checking Maximum Rows Displayed
Following is the example that gets and returns the default number of maximum rows displayed. Interpreter reads this value and displays the rows with this value as upper limit to display.
import pandas as pd print(pd.get_option("display.max_rows"))
Its output is as follows −
60
Example: Checking Maximum Columns Displayed
This example returns the default number of maximum columns displayed. Interpreter reads this value and displays the rows with this value as upper limit to display.
import pandas as pd print(pd.get_option("display.max_columns"))
Its output is as follows −
0
Here, 60 and 0 are the default configuration parameter values.
Setting a New Option
The set_option() function allows you to change the value of a specific parameter, enabling you to customize how data is displayed.
Example: Changing Maximum Rows Displayed
Using set_option(), we can change the default number of rows to be displayed. Here is the example −
import pandas as pd pd.set_option("display.max_rows",10) print(pd.get_option("display.max_rows"))
Its output is as follows −
10
Example: Changing Maximum Columns Displayed
Following is the example that uses the set_option() function to change the default number of columns to be displayed.
import pandas as pd pd.set_option("display.max_columns",30) print(pd.get_option("display.max_columns"))
Its output is as follows −
30
Resetting an Option to Its Default Value
The reset_option() function resets the value of a specified parameter back to its default setting.
Example: Resetting Maximum Rows Displayed
Using the reset_option() function, we can change the value back to the default number of rows to be displayed.
import pandas as pd pd.reset_option("display.max_rows") print(pd.get_option("display.max_rows"))
Its output is as follows −
60
Describing an Option
The describe_option() function provides a description of a specified parameter, explaining what it does and its default value.
Example: Describing Maximum Rows Displayed
This example uses the reset_option() function to get the description of the max_row parameter.
import pandas as pd pd.describe_option("display.max_rows")
Its output is as follows −
display.max_rows : int If max_rows is exceeded, switch to truncate view. Depending on 'large_repr', objects are either centrally truncated or printed as a summary view. 'None' value means unlimited. In case python/IPython is running in a terminal and `large_repr` equals 'truncate' this can be set to 0 and pandas will auto-detect the height of the terminal and print a truncated object which fits the screen height. The IPython notebook, IPython qtconsole, or IDLE do not run in a terminal and hence it is not possible to do correct auto-detection. [default: 60] [currently: 60]
Temporary Option Setting
The option_context() function allows you to set an option temporarily within a with statement. Once the context is exited, the option is automatically reverted to its previous value.
Example: Temporarily Changing Maximum Rows Displayed
This example uses the option_context() function to set the temporarily value for the maximum rows to displayed.
import pandas as pd with pd.option_context("display.max_rows",10): print(pd.get_option("display.max_rows")) print(pd.get_option("display.max_rows"))
Its output is as follows −
10 60
See, the difference between the first and the second print statements. The first statement prints the value set by option_context() which is temporary within the with context itself. After the with context, the second print statement prints the configured value.