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()設置的值,該值在上下文中是臨時的。在使用上下文以後,第二個打印語句打印配置的值。

相關文章
相關標籤/搜索