Pandas 选项和自定义,Pandas 有五个相关函数实现自定义操作,分别是 get_option、set_option、reset_option、describe_option、option_context。
| 函数 | 说明 | 
|---|---|
get_option | 
获取解释器的默认参数值 | 
set_option | 
设置解释器的参数值 | 
reset_option | 
解释器的参数重置为默认值 | 
describe_option | 
打印参数的描述 | 
option_context | 
临时设置解释器的参数,退出使用块时,恢复为默认值 | 
现在来了解函数是如何工作的。
get_option
get_option(param) 获取解释器的默认参数值
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
这里,60和20是默认配置参数值。
set_option
set_option(param, value) 设置解释器的默认参数值
display.max_rows: 设置要显示的默认行数
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:设置要显示的默认列数
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
reset_option(param) 解释器的参数重置为默认值。
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
describe_option(param) 打印参数的描述。
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()设置的值,该值在上下文中是临时的。在使用上下文之后,第二个打印语句打印配置的值。
常用选项
| 编号 | 参数 | 描述 | 
|---|---|---|
| 1 | display.max_rows | 
要显示的最大行数 | 
| 2 | display.max_columns | 
要显示的最大列数 | 
| 3 | display.expand_frame_repr | 
显示数据帧以拉伸页面 | 
| 4 | display.max_colwidth | 
显示最大列宽 | 
| 5 | display.precision | 
显示十进制数的精度 | 
学习笔记
(1) 使用pandas输出data frame 内容时常常会遇到显示不全(部分内容省略)或者在不该换行的地方换行,给我们的观察造成麻烦。可以通过set_option来配置pandas的属性,这里介绍最常用的四个属性,基本上可以满足我们日常的需要。
- display.max_rows 显示的最大行数(避免只显示部分行数据)
 - display.max_columns 显示的最大列数(避免列显示不全)
 - display.max_colwidth 每一列最大的宽度(避免属性值或列名显示不全)
 - display.width 每一行的宽度(避免换行)
 
import warnings
import numpy as np
import pandas as pd
warnings.filterwarnings('ignore')  # 关闭运行时的警告
np.set_printoptions(linewidth=100, suppress=True)   # 打印numpy时设置显示宽度,并且不用科学计数法显示
pd.set_option('display.width', 100)   # pandas设置显示宽度
pd.set_option('precision', 1)   # 设置显示数值的精度
pd.set_option('display.max_columns', 100)   # 设置显示最大列数
pd.set_option('display.max_rows', 120)    # 设置显示最大行数
import pandas as pd
pd.set_option('display.max_rows', 100)
pd.set_option('display.max_columns', 1000)
pd.set_option("display.max_colwidth",1000)
pd.set_option('display.width',1000)
上面代码可以写成一行:
import pandas as pd
pd.set_option('display.max_rows', 100,'display.max_columns', 1000,"display.max_colwidth",1000,'display.width',1000)
极客教程