Python Pandas 简明教程

Python Pandas - Options and Customization

Pandas提供API来定制某些行为,显示最常用的。

Pandas provide API to customize some aspects of its behavior, display is being mostly used.

该API由五个相关函数组成。它们是−

The API is composed of five relevant functions. They are −

  1. get_option()

  2. set_option()

  3. reset_option()

  4. describe_option()

  5. option_context()

现在,我们了解一下函数如何操作。

Let us now understand how the functions operate.

get_option(param)

get_option获取一个单个参数,并返回如下输出所示的值−

get_option takes a single parameter and returns the value as given in the output below −

display.max_rows

显示默认值的数量。解释器读取此值并显示此值的行作为显示的上限。

Displays the default number of value. 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")

它的 output 如下所示 −

Its output is as follows −

60

display.max_columns

显示默认值的数量。解释器读取此值并显示此值的行作为显示的上限。

Displays the default number of value. 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")

它的 output 如下所示 −

Its output is as follows −

20

此处,60 和 20 是默认配置参数值。

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

set_option(param,value)

set_option 接受两个参数并设置参数的值,如下所示:

set_option takes two arguments and sets the value to the parameter as shown below −

display.max_rows

使用@{{s0}},我们可以将默认的显示行数更改为其他数量。

Using set_option(), we can change the default number of rows to be displayed.

import pandas as pd

pd.set_option("display.max_rows",80)

print pd.get_option("display.max_rows")

它的 output 如下所示 −

Its output is as follows −

80

display.max_columns

使用@{{s0}},我们可以将默认的显示行数更改为其他数量。

Using set_option(), we can change the default number of rows to be displayed.

import pandas as pd

pd.set_option("display.max_columns",30)

print pd.get_option("display.max_columns")

它的 output 如下所示 −

Its output is as follows −

30

reset_option(param)

reset_option 接受一个参数并把该值设置回默认值。

reset_option takes an argument and sets the value back to the default value.

display.max_rows

使用 reset_option(),我们可以将值更改回默认显示的行数。

Using reset_option(), 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")

它的 output 如下所示 −

Its output is as follows −

60

describe_option(param)

describe_option 打印参数的描述。

describe_option prints the description of the argument.

display.max_rows

使用 reset_option(),我们可以将值更改回默认显示的行数。

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

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

它的 output 如下所示 −

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]

option_context()

option_context 上下文管理器用于设置 with statement 中的选项。退出 with block 时,选项值会自动恢复。

option_context context manager is used to set the option in with statement temporarily. Option values are restored automatically when you exit the with block

display.max_rows

使用 option_context(),我们可以暂时设置该值。

Using option_context(), we can set the value temporarily.

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"))

它的 output 如下所示 −

Its output is as follows −

10
10

请注意,第一个和第二个打印语句之间的差异。第一个语句打印的是 option_context() 设置的值,它暂时存在于 with context 本身内。在 with context 之后,第二个打印语句打印配置值。

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.

Frequently used Parameters

Sr.No

Parameter & Description

1

display.max_rows Displays maximum number of rows to display

2

2 display.max_columns Displays maximum number of columns to display

3

display.expand_frame_repr Displays DataFrames to Stretch Pages

4

display.max_colwidth Displays maximum column width

5

display.precision Displays precision for decimal numbers