Pandas提供API來自定義其行為的某些方面,大多使用來顯示。
API由五個相關(guān)函數(shù)組成。它們分別是 -
現(xiàn)在來了解函數(shù)是如何工作的。
get_option(param)需要一個參數(shù),并返回下面輸出中給出的值 -
get_option需要一個參數(shù),并返回下面輸出中給出的值 -
display.max_rows
顯示默認(rèn)值。解釋器讀取此值并顯示此值作為顯示上限的行。
import pandas as pd
print ("display.max_rows = ", pd.get_option("display.max_rows"))
執(zhí)行上面示例代碼,得到以下結(jié)果 -
display.max_rows = 60
display.max_columns
顯示默認(rèn)值,解釋器讀取此值并顯示此值作為顯示上限的行。
import pandas as pd
print ("display.max_columns = ", pd.get_option("display.max_columns"))
執(zhí)行上面示例代碼,得到以下結(jié)果 -
display.max_columns = 20
這里,60和20是默認(rèn)配置參數(shù)值。
set_option需要兩個參數(shù),并將該值設(shè)置為指定的參數(shù)值,如下所示:
display.max_rows
使用set_option(),可以更改要顯示的默認(rèn)行數(shù)。
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"))
執(zhí)行上面示例代碼,得到以下結(jié)果 -
before set display.max_rows = 60
after set display.max_rows = 80
display.max_columns
使用set_option(),可以更改要顯示的默認(rèn)行數(shù)。
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"))
執(zhí)行上面示例代碼,得到以下結(jié)果 -
before set display.max_columns = 20
after set display.max_columns = 32
reset_option接受一個參數(shù),并將該值設(shè)置為默認(rèn)值。
display.max_rows
使用reset_option(),可以將該值更改回顯示的默認(rèn)行數(shù)。
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"))
執(zhí)行上面示例代碼,得到以下結(jié)果 -
after set display.max_rows = 32
reset display.max_rows = 60
describe_option打印參數(shù)的描述。
display.max_rows
使用reset_option(),可以將該值更改回顯示的默認(rèn)行數(shù)。
import pandas as pd
pd.describe_option("display.max_rows")
執(zhí)行上面示例代碼,得到以下結(jié)果 -
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上下文管理器用于臨時設(shè)置語句中的選項。當(dāng)退出使用塊時,選項值將自動恢復(fù) -
display.max_rows
使用option_context(),可以臨時設(shè)置該值。
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"))
執(zhí)行上面示例代碼,得到以下結(jié)果 -
10
10
請參閱第一和第二個打印語句之間的區(qū)別。第一個語句打印由option_context()設(shè)置的值,該值在上下文中是臨時的。在使用上下文之后,第二個打印語句打印配置的值。
常用參數(shù),請參考下表 -
| 編號 | 參數(shù) | 描述 |
|---|---|---|
| 1 | display.max_rows |
要顯示的最大行數(shù) |
| 2 | display.max_columns |
要顯示的最大列數(shù) |
| 3 | display.expand_frame_repr |
顯示數(shù)據(jù)幀以拉伸頁面 |
| 4 | display.max_colwidth |
顯示最大列寬 |
| 5 | display.precision |
顯示十進(jìn)制數(shù)的精度 |