Pandas:获取和设置显示、数据行为的选项

2024年8月29日 | 阅读 7 分钟

Pandas 库是一个开源(允许使用)的库,它建立在另一个非常有用的 Python 库 NumPy 之上。Pandas 是一个开源的软件包(或库),为我们提供了高度优化的数据结构和数据分析工具。Pandas 库在数据科学、机器学习和数据分析领域得到了广泛应用,因为它简化了数据导入和数据分析的过程。

Pandas Python 包为我们提供了各种各样的数据结构和操作,有助于轻松处理(添加、更新、删除)数值数据以及时间序列数据。Pandas 包之所以流行的主要原因是它易于导入和易于进行数据分析的功能。Pandas 模块速度非常快,而且由于其高性能和效率,非常实用。现在,一个问题浮现在脑海:Pandas 中有哪些选项和设置?好吧,Pandas 中的选项和设置用于控制 DataFrame 中的数据。让我们通过一个场景来更详细地理解 Pandas 中选项和设置的必要性。

在某些情况下,加载到 DataFrame 中的数据可能有很多截断值,或者某些列被替换为省略号,或者存在一些难以读取的浮点精度。因此,为了应对这些情况,我们有了 Options API 接口。

Pandas 中的 Options API 是什么?

如前所述,为了处理不连贯的数据并自定义诸如行为、数据、分析等各个方面,我们使用 Pandas 模块提供的 Options API 接口。更简单地说,我们可以说 Options API 接口帮助我们修改和配置 DataFrame 的全局行为,如数据、行为等。通过使用 Options API 接口,我们可以直接将 get 和 set 选项作为高级选项属性的属性来使用。

Options API 接口为我们提供了 5 个函数来处理数据:`get_option()`、`set_option()`、`reset_option()`、`describe_option()` 和 `option_context()`。我们将在下一节中详细介绍它们。这些函数以及 Options API 接口的其他功能有助于我们控制 DataFrame 中数据的显示方式。

Pandas 中选项和设置的方法

到目前为止,我们已经讨论了 Pandas 中选项和设置的使用场景及其必要性。现在,让我们通过一些示例来详细了解各种相关函数,以便更清楚。

  • get_option()

`get_option()` 函数用于获取单个选项的值。因此,在我们需要确定 DataFrame 的最大列数的情况下,我们可以使用 `get_option()` 函数。此函数为我们提供了 Pandas 中任何参数的默认值。我们可以查看该值,但不能设置或更改该值(对于设置,我们有另一个函数,我们将在稍后讨论)。`get_option()` 函数接受一个参数。

例如,让我们使用 `display.max_rows` 和 `display.max_columns` 参数来获取 Pandas 模块提供的默认最大行数或列数。

示例

输出

The Maximum number of rows present are = 60
The Maximum number of columns present are = 0
  • set_option()

`set_option()` 函数用于设置单个选项的值。此函数帮助我们将任何参数的默认值设置为任何期望值。`get_option()` 函数接受两个参数,第一个是参数的名称,第二个是参数的值。

  • 更改默认显示设置

现在,要更改 DataFrame 的最大行数或列数,我们可以使用 `set_option()` 函数。

例如,让我们使用 `display.max_rows` 和 `display.max_columns` 参数来更改 Pandas 模块提供的默认最大行数或列数。

示例

输出

By default, the Maximum number of rows present are = 60
By default, the Maximum number of rows present are = 0
The new Maximum number of rows present are = 20
The new Maximum number of columns present are = 20
  • reset_option()

`reset_option()` 函数用于重置单个选项的值。因此,在我们需要重置 DataFrame 的最大行数的情况下,我们可以使用 `reset_option()` 函数。`reset_option()` 函数接受一个参数,即必须将其重置为默认值的参数。

例如,让我们首先使用 `display.max_rows` 和 `display.max_columns` 参数来更改 Pandas 模块提供的默认最大行数和列数。之后,我们将使用 `reset_option()` 函数将值恢复到原始状态。

示例

输出

By default, the Maximum number of rows present are = 60
By default, the Maximum number of rows present are = 0
After resetting the values of the parameters
The default Maximum number of rows present are = 60
The default Maximum number of columns present are =0
  • describe_option()

`describe_option()` 函数用于打印任何参数的完整描述。例如,让我们打印 `display.max_columns` 参数的描述。

示例

输出

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: 0] [currently: 0]
None
  • option_context()

`option_context()` 参数用于在断言期间将可选参数设置为临时值。当离开代码块时,选项的值会自动恢复。简而言之,我们可以说此函数用于调用一个 Pandas 选项,该选项仅在代码块的适应期内有效。

例如,让我们更改 `maximum_row` 参数。

示例

输出

The maximum number of rows present are = 20
The maximum number of rows present are = 20

错误和异常

我们可以将上述函数与 regexp 模式一起用作参数,以匹配模糊的子字符串。如果我们只提供一个匹配多个参数的小型 regex,我们会收到错误 `OptionError`:“Example matched multiple keys”。

例如,如果我们只提供 `max` 作为参数,Python 解释器和 Pandas 模块将无法确定要使用的参数。我们的 `display.max_colwidth`、`display.max_rows` 和 `display.max_columns` 的名称中都包含 `max`,因此仅提供 `max` 将是一个问题。让我们看看错误以获得更清晰的理解。

示例

输出:错误

Traceback (most recent call last):
  File "d:\Personal\Study Material\Scaler Topics Articles\test.py", line 4, in 
    pd.get_option("max")
  File "C:\Users\susha\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\_config\config.py", line 256, in __call__
    return self.__func__(*args, **kwds)
  File "C:\Users\susha\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\_config\config.py", line 128, in _get_option
    key = _get_single_key(pat, silent)
  File "C:\Users\susha\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\_config\config.py", line 116, in _get_single_key
    raise OptionError("Pattern matched multiple keys")
pandas._config.config.OptionError: 'Pattern matched multiple keys'