目录

选项和定制(Options & Customization)

Pandas提供API来定制其行为的某些方面,显示器主要使用。

API由五个相关功能组成。 他们是 -

  • get_option()
  • set_option()
  • reset_option()
  • describe_option()
  • option_context()

现在让我们了解这些功能是如何运作的。

get_option(param)

get_option接受一个参数并返回下面输出中给出的值 -

display.max_rows

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

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

output如下 -

60

display.max_columns

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

import pandas as pd
print pd.get_option("display.max_columns")

output如下 -

20

这里,60和20是默认配置参数值。

set_option(param,value)

set_option接受两个参数并将值设置为参数,如下所示 -

display.max_rows

使用set_option() ,我们可以更改要显示的默认行数。

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

output如下 -

80

display.max_rows

使用set_option() ,我们可以更改要显示的默认行数。

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

output如下 -

30

reset_option(param)

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

display.max_rows

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

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

output如下 -

60

describe_option(param)

describe_option打印参数的描述。

display.max_rows

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

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

output如下 -

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时会自动恢复选项值 -

display.max_rows

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

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如下 -

10
10

请参阅第一个和第二个打印语句之间的区别。 第一个语句打印由option_context()设置的值,该值在with context本身中是临时的。 在with context ,第二个print语句打印配置的值。

经常使用的参数

S.No 参数 描述
1display.max_rows 显示要显示的最大行数
2 2 display.max_columns 显示要显示的最大列数
3display.expand_frame_repr 将数据框显示为拉伸页面
4display.max_colwidth 显示最大列宽
5display.precision 显示十进制数的精度
↑回到顶部↑
WIKI教程 @2018