66_Pandas如何检查和更改选项设置

66_Pandas如何检查和更改选项设置

使用 pandas,可以通过更改选项设置来自定义行为和显示。解释如何检查和更改各种设置值。

在此对以下内容进行说明。

  • 访问属性以检查和更改设置:options
  • 以列表形式显示配置信息:describe_option()
  • 使用函数检查和更改设置值:get_option()、set_option()
  • 一次检查并更改多个设置值
  • 恢复默认设置:reset_option()
  • 临时更改 with 块中的设置: option_context()
import pandas as pd
import pprint

print(pd.__version__)
# 0.23.0

访问属性以检查和更改设置:options

可以使用 pd.options 下的属性来访问、检查和更改每个设置值。

print(pd.options.display.max_rows)
# 60

pd.options.display.max_rows = 100

print(pd.options.display.max_rows)
# 100

在Jupyter Notebook或者支持补全的编辑器中,可以使用TAB键显示补全建议,很方便。 您还可以使用内置函数 dir() 检查正下方有哪些项目。

print(dir(pd.options))
# ['compute', 'display', 'html', 'io', 'mode', 'plotting']

pprint.pprint(dir(pd.options.display))
# ['chop_threshold',
#  'colheader_justify',
#  'column_space',
#  'date_dayfirst',
#  'date_yearfirst',
#  'encoding',
#  'expand_frame_repr',
#  'float_format',
#  'html',
#  'large_repr',
#  'latex',
#  'max_categories',
#  'max_columns',
#  'max_colwidth',
#  'max_info_columns',
#  'max_info_rows',
#  'max_rows',
#  'max_seq_items',
#  'memory_usage',
#  'multi_sparse',
#  'notebook_repr_html',
#  'pprint_nest_depth',
#  'precision',
#  'show_dimensions',
#  'unicode',
#  'width']

以列表形式显示配置信息:describe_option()

使用 pd.describe_option() 函数可以显示每个设置项内容的描述、默认值和当前值。

如果省略该参数,将显示所有设置项的信息。由于量较大,这里省略输出。

pd.describe_option()

指定正则表达式模式字符串作为参数。将显示与模式匹配的设置项目的信息。 如果在正则表达式中不使用特殊字符而指定简单字符串,则将显示包含该字符串的设置项目。

pd.describe_option('compute')
# compute.use_bottleneck : bool
#     Use the bottleneck library to accelerate if it is installed,
#     the default is True
#     Valid values: False,True
#     [default: True] [currently: True]
# compute.use_numexpr : bool
#     Use the numexpr library to accelerate computation if it is installed,
#     the default is True
#     Valid values: False,True
#     [default: True] [currently: True]

pd.describe_option('max_col')
# display.max_columns : int
#     If max_cols 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 width of the terminal and print a truncated object which fits
#     the screen width. 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: 20] [currently: 20]
# display.max_colwidth : int
#     The maximum width in characters of a column in the repr of
#     a pandas data structure. When the column overflows, a "..."
#     placeholder is embedded in the output.
#     [default: 50] [currently: 50]

每个设置项末尾的[default: xxx] [currently: xxx]为默认值和当前值。 还可以在正则表达式中使用特殊字符。

pd.describe_option('max.*col')
# display.max_columns : int
#     If max_cols 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 width of the terminal and print a truncated object which fits
#     the screen width. 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: 20] [currently: 20]
# display.max_colwidth : int
#     The maximum width in characters of a column in the repr of
#     a pandas data structure. When the column overflows, a "..."
#     placeholder is embedded in the output.
#     [default: 50] [currently: 50]
# display.max_info_columns : int
#     max_info_columns is used in DataFrame.info method to decide if
#     per column information will be printed.
#     [default: 100] [currently: 100]

使用函数检查和更改设置值:get_option()、set_option()

除了使用上述属性访问设置值外,还可以使用函数检查和更改它们。

指定正则表达式模式字符串作为参数。返回与模式匹配的设置项的当前值。

print(pd.get_option('display.max_rows'))
# 100

指定第一个参数的正则表达式模式字符串以及要为第二个参数设置的值。与模式匹配的设置项目的值更改为指定值。

pd.set_option('display.max_rows', 60)

由于它是使用正则表达式模式指定的,因此不需要指定完整的项目名称,但如果多个项目匹配,则会出现 OptionError 错误。

print(pd.get_option('max_r'))
# 60

pd.set_option('max_r', 100)

# pd.get_option('max')
# OptionError: 'Pattern matched multiple keys'

# pd.set_option('max', 60)
# OptionError: 'Pattern matched multiple keys'

如果指定除完整项目名称以外的任何内容,则将来匹配新添加的项目时有可能会发生错误,因此对于将长期使用的代码最好使用完整项目名称。

一次检查并更改多个设置值

如上所述,使用属性的方法以及函数 get_option() 和 set_option() 一次只能访问一个设置值,官方没有办法一次检查或更改多个设置值。

l = ['display.max_rows', 'display.max_columns', 'display.max_colwidth']

print([pd.get_option(i) for i in l])
# [100, 20, 50]

print({i: pd.get_option(i) for i in l})
# {'display.max_rows': 100, 'display.max_columns': 20, 'display.max_colwidth': 50}

从项目名称和所需值的字典中一次更改和检查多个设置值。使用字典方法 items() 和keys()。

d = {'display.max_rows': 80,
     'display.max_columns': 80,
     'display.max_colwidth': 80}

[pd.set_option(k, v) for k, v in d.items()]

print({i: pd.get_option(i) for i in d.keys()})
# {'display.max_rows': 80, 'display.max_columns': 80, 'display.max_colwidth': 80}

恢复默认设置:reset_option()

返回默认设置的函数是reset_option()。

指定正则表达式模式字符串作为参数。与模式匹配的设置项目的值将重置为默认值。

print(pd.options.display.max_rows)
# 80

pd.reset_option('display.max_rows')

print(pd.options.display.max_rows)
# 60

匹配多个项目就可以了。所有匹配的项目都将被重置。

print(pd.options.display.max_columns)
print(pd.options.display.max_colwidth)
# 80
# 80

pd.reset_option('max_col')

print(pd.options.display.max_columns)
print(pd.options.display.max_colwidth)
# 20
# 50

还可以使用与开头匹配的正则表达式特殊字符 ^ 来重置显示屏上的所有项目。

pd.options.display.max_rows = 100
pd.options.display.max_columns = 100
pd.options.display.max_colwidth = 100

pd.reset_option('^display', silent=True)

print(pd.options.display.max_rows)
print(pd.options.display.max_columns)
print(pd.options.display.max_colwidth)
# 60
# 20
# 50

如果参数为“all”,则所有项目都将被重置。由于所有项目均已访问,因此根据版本的不同,可能会出现 FutureWarning。

pd.reset_option('all')
# html.border has been deprecated, use display.html.border instead
# (currently both are identical)
# : boolean
#     use_inf_as_null had been deprecated and will be removed in a future
#     version. Use `use_inf_as_na` instead.
# /usr/local/lib/python3.6/site-packages/pandas/core/config.py:619: FutureWarning: html.border has been deprecated, use display.html.border instead
# (currently both are identical)
#   warnings.warn(d.msg, FutureWarning)
# /usr/local/lib/python3.6/site-packages/pandas/core/config.py:619: FutureWarning: 
# : boolean
#     use_inf_as_null had been deprecated and will be removed in a future
#     version. Use `use_inf_as_na` instead.
#   warnings.warn(d.msg, FutureWarning)

如果不希望输出 FutureWarning,请同时指定silent=True 参数。

pd.reset_option('all', silent=True)

临时更改 with 块中的设置: option_context()

如果在 with 块中使用 pd.option_context() ,则设置将仅在该块内更改。

指定第一个参数的正则表达式模式字符串以及要为第二个参数设置的值。与模式匹配的设置项目的值更改为指定值。 设置仅在 with 块内更改,并在退出块时返回到原始值。

with pd.option_context('display.max_rows', 100):
    print(pd.options.display.max_rows)
# 100

print(pd.options.display.max_rows)
# 60

它不会返回默认值,而是返回进入 with 块之前的值。

pd.options.display.max_rows = 80

with pd.option_context('display.max_rows', 100):
    print(pd.options.display.max_rows)
# 100

print(pd.options.display.max_rows)
# 80

通过重复指定正则表达式模式字符串(pat、val、pat、val、…)和设置值作为参数,可以更改多个设置项目。

with pd.option_context('display.max_rows', 100, 'display.max_columns', 100):
    print(pd.options.display.max_rows)
    print(pd.options.display.max_columns)
# 100
# 100

print(pd.options.display.max_rows)
print(pd.options.display.max_columns)
# 80
# 20

在 with 块之外使用 pd.option_context() 不会更改该值。

pd.option_context('display.max_rows', 100)

print(pd.options.display.max_rows)
# 80
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值