前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >8. Pandas系列 - 选项和自定义

8. Pandas系列 - 选项和自定义

作者头像
Python编程爱好者
发布2020-11-04 16:03:04
4130
发布2020-11-04 16:03:04
举报
  • get_option()
  • set_option()
  • reset_option()
  • describe_option()
  • option_context()

自定义其行为属性设置

API由五个相关函数:

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

编号

参数

描述

1

display.max_rows

要显示的最大行数

2

display.max_columns

要显示的最大列数

3

display.expand_frame_repr

显示数据帧以拉伸页面

4

display.max_colwidth

显示最大列宽

5

display.precision

显示十进制数的精度

get_option()

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

代码语言:javascript
复制
import pandas as pd
print ("display.max_rows = ", pd.get_option("display.max_rows"))
print ("display.max_columns = ", pd.get_option("display.max_columns"))

res:

代码语言:javascript
复制
('display.max_rows = ', 100)
('display.max_columns = ', 32)

set_option()

代码语言:javascript
复制
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"))

res:

代码语言:javascript
复制
before set display.max_rows =  60
after set display.max_rows =  80

reset_option()

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

代码语言:javascript
复制
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"))

res:

代码语言:javascript
复制
after set display.max_rows =  32
reset display.max_rows =  60

describe_option()

describe_option打印参数的描述。

代码语言:javascript
复制
import pandas as pd

pd.describe_option("display.max_rows")

res:

代码语言:javascript
复制
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: 100]

option_context()

option_context上下文管理器用于临时设置语句中的选项。当退出使用块时,选项值将自动恢复

代码语言:javascript
复制
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"))

res:

代码语言:javascript
复制
10
10

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

作者:Johngo

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-10-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 计算广告生态 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • get_option()
  • set_option()
  • reset_option()
  • describe_option()
  • option_context()
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档