Pandas | 12 选项和自定义

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

 

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

  • 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(param)

get_option(param)须要一个参数,并返回给出的值。函数

  • display.max_rows显示默认值。解释器读取此值并显示此值做为显示上限的行。
  • display.max_columns显示默认值,解释器读取此值并显示此值做为显示上限的行。
import pandas as pd

print ("display.max_rows = ", pd.get_option("display.max_rows"))
print ("display.max_columns = ", pd.get_option("display.max_columns"))

输出结果 -this

display.max_rows = 60
display.max_columns = 20spa

 

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

 

set_option(param,value)

set_option须要两个参数,并将该值设置为指定的参数值.blog

import pandas as pd

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


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

输出结果:ci

before set display.max_rows = 60
before set display.max_columns = 20terminal


after set display.max_rows = 80
after set display.max_columns = 42

 
 

reset_option(param)

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打印参数的描述。

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

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 60
 

第一个语句打印由option_context()设置的值,该值在上下文中是临时的。在使用上下文以后,第二个打印语句打印配置的值。

相关文章
相关标签/搜索