pandas 教程

Pandas 数据结构

Pandas 基本操作

Pandas API

original icon
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.knowledgedict.com/tutorial/pandas-options-and-customization.html

Pandas选项和自定义


Pandas 提供 API 来自定义其行为的某些方面,大多使用来显示。

API 由五个相关函数组成。它们分别是 -

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

现在来了解函数是如何工作的。

get_option(param)

get_option(param)需要一个参数,并返回下面输出中给出的值 -

get_option需要一个参数,并返回下面输出中给出的值 -

display.max_rows

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

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

执行上面示例代码,得到以下结果 -

display.max_rows =  60

display.max_columns

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

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

执行上面示例代码,得到以下结果 -

display.max_columns =  20

这里,6020是默认配置参数值。

set_option(param,value)

set_option需要两个参数,并将该值设置为指定的参数值,如下所示:

display.max_rows

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

import pandas as pd

print ("before set display.max_rows = ", pd.get_option("display.max_rows")) 

pd.set_option("display.max_rows",80)
print ("after set display.max_rows = ", pd.get_option("display.max_rows"))

执行上面示例代码,得到以下结果 -

before set display.max_rows =  60
after set display.max_rows =  80

display.max_columns

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

import pandas as pd

print ("before set display.max_columns = ", pd.get_option("display.max_columns")) 

pd.set_option("display.max_columns",32)
print ("after set display.max_columns = ", pd.get_option("display.max_columns"))

执行上面示例代码,得到以下结果 -

before set display.max_columns =  20
after set display.max_columns =  32

reset_option(param)

reset_option接受一个参数,并将该值设置为默认值。

display.max_rows

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

import pandas as pd

pd.set_option("display.max_rows",32)
print ("after set display.max_rows = ", pd.get_option("display.max_rows")) 

pd.reset_option("display.max_rows")
print ("reset display.max_rows = ", pd.get_option("display.max_rows"))

执行上面示例代码,得到以下结果 -

after set display.max_rows =  32
reset display.max_rows =  60

describe_option(param)

describe_option打印参数的描述。

display.max_rows

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

import pandas as pd

pd.describe_option("display.max_rows")

执行上面示例代码,得到以下结果 -

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上下文管理器用于临时设置语句中的选项。当退出使用块时,选项值将自动恢复 -

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

执行上面示例代码,得到以下结果 -

10
10

请参阅第一和第二个打印语句之间的区别。第一个语句打印由option_context()设置的值,该值在上下文中是临时的。在使用上下文之后,第二个打印语句打印配置的值。

常用参数,请参考下表 -

编号 参数 描述
1 display.max_rows 要显示的最大行数
2 display.max_columns 要显示的最大列数
3 display.expand_frame_repr 显示数据帧以拉伸页面
4 display.max_colwidth 显示最大列宽
5 display.precision 显示十进制数的精度

在本章中,我们将使用基本系列/索引来讨论字符串操作。在随后的章节中,将学习如何将这些字符串函数应用于数据帧(DataFrame)。 ...
在本章中,我们将讨论如何切割和丢弃日期,并获取Pandas中大对象的子集。 ...
在Django中,你可以通过自定义admin页面来扩展和改进Django默认提供的管理界面。py`文件中,添加以下代码:这里我们使用了`@s ...
Pandas对象之间的基本迭代的行为取决于类型。当迭代一个系列时,它被视为数组式,基本迭代产生这些值。其他数据结构,如:DataFrame和 ...
当有了滚动,扩展和ewm对象创建了以后,就有几种方法可以对数据执行聚合。 ...