pandas.set_option#

pandas.set_option(pat, value) = <pandas._config.config.CallableDynamicDoc object>#

设置指定选项的值。

可用选项:

  • compute.[use_bottleneck, use_numba, use_numexpr]

  • display.[chop_threshold, colheader_justify, date_dayfirst, date_yearfirst, encoding, expand_frame_repr, float_format]

  • display.html.[border, table_schema, use_mathjax]

  • display.[large_repr, max_categories, max_columns, max_colwidth, max_dir_items, max_info_columns, max_info_rows, max_rows, max_seq_items, memory_usage, min_rows, multi_sparse, notebook_repr_html, pprint_nest_depth, precision, show_dimensions]

  • display.unicode.[ambiguous_as_wide, east_asian_width]

  • display.[width]

  • future.[infer_string, no_silent_downcasting]

  • io.excel.ods.[reader, writer]

  • io.excel.xls.[reader]

  • io.excel.xlsb.[reader]

  • io.excel.xlsm.[reader, writer]

  • io.excel.xlsx.[reader, writer]

  • io.hdf.[default_format, dropna_table]

  • io.parquet.[engine]

  • io.sql.[engine]

  • mode.[chained_assignment, copy_on_write, data_manager, sim_interactive, string_storage, use_inf_as_na]

  • plotting.[backend]

  • plotting.matplotlib.[register_converters]

  • styler.format.[decimal, escape, formatter, na_rep, precision, thousands]

  • styler.html.[mathjax]

  • styler.latex.[environment, hrules, multicol_align, multirow_align]

  • styler.render.[encoding, max_columns, max_elements, max_rows, repr]

  • styler.sparse.[columns, index]

Parameters:
patstr

应该匹配单个选项的正则表达式。注意:为了方便起见,支持部分匹配,但除非您使用完整的选项名称(例如 x.y.z.option_name),否则在将来的版本中,如果引入了名称相似的新选项,您的代码可能会中断。

valueobject

选项的新值。

Returns:
None
Raises:
如果不存在该选项,则为 OptionError。

Notes

有关更多信息,请参阅 User Guide

可用选项及其描述:

compute.use_bottleneckbool

如果安装了 bottleneck 库,则使用它来加速计算,默认值为 True。有效值:False,True [默认值: True] [当前值: True]

compute.use_numbabool

如果安装了 numba 库,则为某些操作使用 numba 引擎选项,默认值为 False。有效值:False,True [默认值: False] [当前值: False]

compute.use_numexprbool

如果安装了 numexpr 库,则使用它来加速计算,默认值为 True。有效值:False,True [默认值: True] [当前值: True]

display.chop_thresholdfloat 或 None

如果设置为浮点数值,则所有小于给定阈值的浮点数值在 repr 中将被显示为 0。[默认值: None] [当前值: None]

display.colheader_justify‘left’/’right’

控制列标题的对齐方式。由 DataFrameFormatter 使用。[默认值: right] [当前值: right]

display.date_dayfirstboolean

如果为 True,则日期将按日优先打印和解析,例如 20/01/2005。[默认值: False] [当前值: False]

display.date_yearfirstboolean

如果为 True,则日期将按年优先打印和解析,例如 2005/01/20。[默认值: False] [当前值: False]

display.encodingstr/unicode

默认为检测到的控制台编码。指定字符串返回 to_string 时要使用的编码,这些字符串通常是要显示在控制台上的字符串。[默认值: utf-8] [当前值: utf-8]

display.expand_frame_reprboolean

是否为宽 DataFrame 打印完整的 DataFrame repr 并在多行显示,max_columns 仍会生效,但如果输出宽度超出 display.width,则会在多个“页面”之间换行。[默认值: True] [当前值: True]

display.float_formatcallable

callable 应该接受一个浮点数并返回一个所需格式的字符串。这在一些地方(如 SeriesFormatter)会用到。请参阅 formats.format.EngFormatter 获取示例。[默认值: None] [当前值: None]

display.html.borderint

一个 border=value 属性将被插入到 DataFrame HTML repr 的 <table> 标签中。[默认值: 1] [当前值: 1]

display.html.table_schemaboolean

是否为支持它的前端发布 Table Schema 表示。 (默认: False) [默认值: False] [当前值: False]

display.html.use_mathjaxboolean

如果为 True,Jupyter notebook 将使用 MathJax 处理表格内容,渲染美元符号括起来的数学表达式。(默认: True) [默认值: True] [当前值: True]

display.large_repr‘truncate’/’info’

对于超过 max_rows/max_cols 的 DataFrame,repr(和 HTML repr)可以显示一个截断的表格,或者切换到 df.info() 的视图(这是 pandas 早期版本的行为)。[默认值: truncate] [当前值: truncate]

display.max_categoriesint

这设置了 pandas 在打印 Categorical 或 dtype 为 “category” 的 Series 时应输出的最大类别数。[默认值: 8] [当前值: 8]

display.max_columnsint

如果超过 max_cols,则切换到截断视图。取决于 large_repr,对象要么被中心截断,要么以摘要视图打印。“None”值表示无限制。

如果 python/IPython 在终端中运行且 large_repr 等于 ‘truncate’,则可以将其设置为 0 或 None,pandas 将自动检测终端的宽度并打印一个适合屏幕宽度的截断对象。IPython notebook、IPython qtconsole 或 IDLE 不在终端中运行,因此无法进行正确自动检测,默认为 20。[默认值: 0] [当前值: 0]

display.max_colwidthint 或 None

pandas 数据结构 repr 中一列的最大宽度(以字符为单位)。当列溢出时,将在输出中嵌入“…”占位符。“None”值表示无限制。[默认值: 50] [当前值: 50]

display.max_dir_itemsint

添加到 dir(…) 中的项目数。“None”值表示无限制。由于 dir 是缓存的,更改此选项不会立即影响已存在的 DataFrame,直到删除或添加列。

这被用于建议从 DataFrame 中进行制表符补全的列。 [默认值: 100] [当前值: 100]

display.max_info_columnsint

max_info_columns 用于 DataFrame.info 方法,以决定是否打印每列的信息。 [默认值: 100] [当前值: 100]

display.max_info_rowsint

df.info() 通常会显示每列的 null 计数。对于大型的 frame,这可能会很慢。max_info_rows 和 max_info_cols 仅将此 null 检查限制在小于指定维度的 frame 上。 [默认值: 1690785] [当前值: 1690785]

display.max_rowsint

如果超过 max_rows,则切换到截断视图。根据 large_repr,对象将被集中截断或打印为摘要视图。’None’ 值表示无限制。

如果 python/IPython 在终端中运行且 large_repr 等于 ‘truncate’,则可以将其设置为 0,pandas 将自动检测终端的高度并打印一个适合屏幕高度的截断对象。IPython notebook、IPython qtconsole 或 IDLE 不在终端中运行,因此无法进行正确的自动检测。 [默认值: 60] [当前值: 60]

display.max_seq_itemsint 或 None

在美观打印长序列时,最多只能打印 max_seq_items 个项。如果省略了某些项,则会在最终字符串中用“…”表示。

如果设置为 None,则要打印的项数没有限制。 [默认值: 100] [当前值: 100]

display.memory_usagebool, string or None

这指定了在调用 df.info() 时是否应显示 DataFrame 的内存使用情况。有效值:True、False、’deep’ [默认值: True] [当前值: True]

display.min_rowsint

在截断视图中要显示的行数(当 max_rows 被超过时)。当 max_rows 设置为 None 或 0 时忽略。设置为 None 时,则遵循 max_rows 的值。 [默认值: 10] [当前值: 10]

display.multi_sparseboolean

“稀疏化” MultiIndex 显示(不在组内显示外层重复的元素) [默认值: True] [当前值: True]

display.notebook_repr_htmlboolean

当为 True 时,IPython notebook 将为 pandas 对象使用 html 表示(如果可用)。 [默认值: True] [当前值: True]

display.pprint_nest_depthint

控制美观打印时要处理的嵌套级别数 [默认值: 3] [当前值: 3]

display.precisionint

Floating point output precision in terms of number of places after the decimal, for regular formatting as well as scientific notation. Similar to precision in numpy.set_printoptions(). [default: 6] [currently: 6]

display.show_dimensionsboolean or ‘truncate’

在 DataFrame repr 结尾打印尺寸。如果指定为 ‘truncate’,则仅在 frame 被截断时(例如,未显示所有行和/或列)才打印尺寸 [默认值: truncate] [当前值: truncate]

display.unicode.ambiguous_as_wideboolean

是否使用 Unicode 东亚宽度来计算显示文本宽度。启用此项可能会影响性能(默认:False) [默认值: False] [当前值: False]

display.unicode.east_asian_widthboolean

是否使用 Unicode 东亚宽度来计算显示文本宽度。启用此项可能会影响性能(默认:False) [默认值: False] [当前值: False]

display.widthint

显示宽度(以字符为单位)。如果 python/IPython 在终端中运行,则可以将其设置为 None,pandas 将正确自动检测宽度。请注意,IPython notebook、IPython qtconsole 或 IDLE 不在终端中运行,因此无法正确检测宽度。 [默认值: 80] [当前值: 80]

future.infer_string 是否推断字符串序列对象为 pyarrow 字符串 dtype,这将在 pandas 3.0 中成为默认行为(届时此选项将被弃用)。

[默认值: False] [当前值: False]

future.no_silent_downcasting 是否选择加入未来行为,该行为将*不*会自动向下转换 Series 和 DataFrame 的 wheremaskclip 方法的结果。静默向下转换将在 pandas 3.0 中删除(届时此选项将被弃用)。

[默认值: False] [当前值: False]

io.excel.ods.readerstring

‘ods’ 文件的默认 Excel 读取引擎。可用选项:auto、odf、calamine。 [默认值: auto] [当前值: auto]

io.excel.ods.writerstring

‘ods’ 文件的默认 Excel 写入引擎。可用选项:auto、odf。 [默认值: auto] [当前值: auto]

io.excel.xls.readerstring

‘xls’ 文件的默认 Excel 读取引擎。可用选项:auto, xlrd, calamine。[默认值:auto] [当前值:auto]

io.excel.xlsb.readerstring

‘xlsb’ 文件的默认 Excel 读取引擎。可用选项:auto, pyxlsb, calamine。[默认值:auto] [当前值:auto]

io.excel.xlsm.readerstring

‘xlsm’ 文件的默认 Excel 读取引擎。可用选项:auto, xlrd, openpyxl, calamine。[默认值:auto] [当前值:auto]

io.excel.xlsm.writerstring

‘xlsm’ 文件的默认 Excel 写入引擎。可用选项:auto, openpyxl。[默认值:auto] [当前值:auto]

io.excel.xlsx.readerstring

‘xlsx’ 文件的默认 Excel 读取引擎。可用选项:auto, xlrd, openpyxl, calamine。[默认值:auto] [当前值:auto]

io.excel.xlsx.writerstring

‘xlsx’ 文件的默认 Excel 写入引擎。可用选项:auto, openpyxl, xlsxwriter。[默认值:auto] [当前值:auto]

io.hdf.default_formatformat

默认写入格式,如果为 None,则 put 默认值为 ‘fixed’,append 默认值为 ‘table’ [默认值:None] [当前值:None]

io.hdf.dropna_tableboolean

追加到表时删除所有 NaN 行 [默认值:False] [当前值:False]

io.parquet.enginestring

默认的 parquet 读取/写入引擎。可用选项:’auto’, ‘pyarrow’, ‘fastparquet’,默认值为 ‘auto’ [默认值:auto] [当前值:auto]

io.sql.enginestring

默认的 sql 读取/写入引擎。可用选项:’auto’, ‘sqlalchemy’,默认值为 ‘auto’ [默认值:auto] [当前值:auto]

mode.chained_assignmentstring

尝试使用链式赋值时引发异常、发出警告或不执行任何操作。默认值为 warn [默认值:warn] [当前值:warn]

mode.copy_on_writebool

使用 Copy-on-Write 使用新的 copy-view 行为。默认为 False,除非被 ‘PANDAS_COPY_ON_WRITE’ 环境变量覆盖(如果设置为 “1” 表示 True,需要在导入 pandas 之前设置)。 [默认值:False] [当前值:False]

mode.data_managerstring

内部数据管理器类型;可以是 “block” 或 “array”。默认为 “block”,除非被 ‘PANDAS_DATA_MANAGER’ 环境变量覆盖(需要在导入 pandas 之前设置)。 [默认值:block] [当前值:block] (已弃用,请改用 ``。)

mode.sim_interactiveboolean

用于测试目的,是否模拟交互模式 [默认值:False] [当前值:False]

mode.string_storagestring

StringDtype 的默认存储。 [默认值:auto] [当前值:auto]

mode.use_inf_as_naboolean

True 表示将 None、NaN、INF、-INF 视为 NA(旧方式),False 表示 None 和 NaN 为 null,但 INF、-INF 不是 NA(新方式)。

此选项在 pandas 2.1.0 中已弃用,并将在 3.0 中移除。[默认值:False] [当前值:False] (已弃用,请改用 ``。)

plotting.backendstr

要使用的绘图后端。默认值为 “matplotlib”,即 pandas 提供的后端。可以通过提供实现后端模块的名称来指定其他后端。 [默认值:matplotlib] [当前值:matplotlib]

plotting.matplotlib.register_convertersbool 或 ‘auto’。

是否为日期、时间、日期时间以及 Period 注册 matplotlib 的 units 注册表的转换器。将其设置为 False 将删除转换器,恢复 pandas 覆盖的任何转换器。 [默认值:auto] [当前值:auto]

styler.format.decimalstr

浮点数和复数的十进制分隔符字符表示。 [默认值:.] [当前值:.]

styler.format.escapebool, default False

是否根据给定上下文(html 或 latex)转义某些字符。 [默认值:None] [当前值:None]

styler.format.formatterstr, callable, dict, optional

将用作 Styler.format 中默认的格式化对象。 [默认值:None] [当前值:None]

styler.format.na_repbool, default False

标识为缺失值的字符串表示。 [默认值:None] [当前值:None]

styler.format.precisionint

浮点数和复数的精度。 [默认值:6] [当前值:6]

styler.format.thousandsbool, default False

浮点数、整数和复数的千位分隔符字符表示。 [默认值:None] [当前值:None]

styler.html.mathjaxbool

如果为 False,将在 Jupyter Notebook 中为表格属性渲染不使用 Mathjax 的特殊 CSS 类。 [默认值:True] [当前值:True]

styler.latex.environmentstr

用于替换 \\begin{table} 的环境。如果使用 longtable,则会生成特定的 longtable 环境格式。[默认值:None][当前值:None]

styler.latex.hrulesbool

是否在顶部、底部和标题下方添加水平线。[默认值:False][当前值:False]

styler.latex.multicol_align{“r”, “c”, “l”, “naive-l”, “naive-r”}

稀疏化 LaTeX 多列的水平对齐说明符。还可以为非朴素值添加管道分隔符以绘制垂直线,例如 “|r” 将在右对齐合并单元格的左侧绘制一条线。[默认值:r][当前值:r]

styler.latex.multirow_align{“c”, “t”, “b”}

稀疏化 LaTeX 多行的垂直对齐说明符。[默认值:c][当前值:c]

styler.render.encodingstr

输出 HTML 和 LaTeX 文件时使用的编码。[默认值:utf-8][当前值:utf-8]

styler.render.max_columnsint, optional

将要渲染的最大列数。仍可能因满足 max_elements 而减少,max_elements 具有优先权。[默认值:None][当前值:None]

styler.render.max_elementsint

将要渲染的最大数据单元格 (<td>) 数量,在此之后将根据需要对列、行或两者进行截断。[默认值:262144][当前值:262144]

styler.render.max_rowsint, optional

将要渲染的最大行数。仍可能因满足 max_elements 而减少,max_elements 具有优先权。[默认值:None][当前值:None]

styler.render.reprstr

在 Jupyter Notebook 中确定使用哪种输出({“html”, “latex”})。[默认值:html][当前值:html]

styler.sparse.columnsbool

是否稀疏化显示分层列。设置为 False 将为每一列的分层键显示每个显式级别元素。[默认值:True][当前值:True]

styler.sparse.indexbool

是否稀疏化显示分层索引。设置为 False 将为每一行的分层键显示每个显式级别元素。[默认值:True][当前值:True]

Examples

>>> pd.set_option('display.max_columns', 4)
>>> df = pd.DataFrame([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>>> df
   0  1  ...  3   4
0  1  2  ...  4   5
1  6  7  ...  9  10
[2 rows x 5 columns]
>>> pd.reset_option('display.max_columns')