天天看點

8. Pandas系列 - 選項和自定義

  • 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需要一個參數,并傳回下面輸出中給出的值

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:

('display.max_rows = ', 100)
('display.max_columns = ', 32)
           

複制

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

複制

res:

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

複制

reset_option()

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

複制

res:

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

複制

describe_option()

describe_option列印參數的描述。

import pandas as pd

pd.describe_option("display.max_rows")
           

複制

res:

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上下文管理器用于臨時設定語句中的選項。當退出使用塊時,選項值将自動恢複

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:

10
10
           

複制

請參閱第一和第二個列印語句之間的差別。第一個語句列印由option_context()設定的值,該值在上下文中是臨時的。在使用上下文之後,第二個列印語句列印配置的值。

作者:Johngo