Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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.NoParameter & 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

Theget_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 pdprint(pd.get_option("display.max_rows"))

Itsoutput 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 pdprint(pd.get_option("display.max_columns"))

Itsoutput is as follows −

0

Here, 60 and 0 are the default configuration parameter values.

Setting a New Option

Theset_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

Usingset_option(), we can change the default number of rows to be displayed. Here is the example −

import pandas as pdpd.set_option("display.max_rows",10)print(pd.get_option("display.max_rows"))

Itsoutput is as follows −

10

Example: Changing Maximum Columns Displayed

Following is the example that uses theset_option() function to change the default number of columns to be displayed.

import pandas as pdpd.set_option("display.max_columns",30)print(pd.get_option("display.max_columns"))

Itsoutput is as follows −

30

Resetting an Option to Its Default Value

Thereset_option() function resets the value of a specified parameter back to its default setting.

Example: Resetting Maximum Rows Displayed

Using thereset_option() function, we can change the value back to the default number of rows to be displayed.

import pandas as pdpd.reset_option("display.max_rows")print(pd.get_option("display.max_rows"))

Itsoutput is as follows −

60

Describing an Option

Thedescribe_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 thereset_option() function to get the description of themax_row parameter.

import pandas as pdpd.describe_option("display.max_rows")

Itsoutput 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

Theoption_context() function allows you to set an option temporarily within awith statement. Once the context is exited, the option is automatically reverted to its previous value.

Example: Temporarily Changing Maximum Rows Displayed

This example uses theoption_context() function to set the temporarily value for the maximum rows to displayed.

import pandas as pdwith pd.option_context("display.max_rows",10):   print(pd.get_option("display.max_rows"))print(pd.get_option("display.max_rows"))

Itsoutput is as follows −

1060

See, the difference between the first and the second print statements. The first statement prints the value set byoption_context() which is temporary within thewith context itself. After thewith context, the second print statement prints the configured value.

Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp