Pandas 設(shè)置數(shù)據(jù)顯示格式

2022-07-13 11:54 更新

在用 Pandas 做數(shù)據(jù)分析的過程中,總需要打印數(shù)據(jù)分析的結(jié)果,如果數(shù)據(jù)體量較大就會存在輸出內(nèi)容不全(部分內(nèi)容省略)或者換行錯誤等問題。Pandas 為了解決上述問題,允許你對數(shù)據(jù)顯示格式進行設(shè)置。下面列出了五個用來設(shè)置顯示格式的函數(shù),分別是:

  • get_option()
  • set_option()
  • reset_option()
  • describe_option()
  • option_context()

它們的功能介紹如下:

函數(shù)名稱 說明
get_option 獲取解釋器的默認參數(shù)值。
set_option 更改解釋器的默認參數(shù)值。
reset_option 解釋器的參數(shù)重置為默認值。
describe_option 輸出參數(shù)的描述信息。
option_context 臨時設(shè)置解釋器參數(shù),當退出使用的語句塊時,恢復為默認值。

下面對上述函數(shù)分別進行介紹。

get_option()

該函數(shù)接受單一參數(shù),用來獲取顯示上限的行數(shù)或者列數(shù),示例如下:

1) display.max_rows

獲取顯示上限的行數(shù),示例如下:

import pandas as pd
print (pd.get_option("display.max_rows"))

輸出結(jié)果:

60

2) display.max_columns

獲取顯示上限的列數(shù),示例如下:

import pandas as pd
print (pd.get_option("display.max_columns"))

輸出結(jié)果:

20

由此可知,默認值顯示上限是(60,20)。

set_option()

該函數(shù)用來更改要默認顯示的行數(shù)和列數(shù),示例如下:

1) 修改默認行數(shù)

import pandas as pd
pd.set_option("display.max_rows",70)
print (pd.get_option("display.max_rows"))

輸出結(jié)果:

70

2) 修改默認列數(shù)

import pandas as pd
pd.set_option("display.max_columns",40)
print (pd.get_option("display.max_columns"))

輸出結(jié)果:

40

reset_option()

該方法接受一個參數(shù),并將修改后的值設(shè)置回默認值。示例如下:

import pandas as pd
pd.reset_option("display.max_rows")
#恢復為默認值
print(pd.get_option("display.max_rows"))

輸出結(jié)果:

60

describe_option()

該方法輸出參數(shù)的描述信息。示例如下:

import pandas as pd
pd.describe_option("display.max_rows")

輸出結(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()

option_context() 上下文管理器,用于臨時設(shè)置 with 語句塊中的默認顯示參數(shù)。當您退出 with 語句塊時,參數(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"))

輸出結(jié)果:

10
60

注意:第一個 Print 語句打印 option_context() 設(shè)置的臨時值。當退出 with 語句塊時,第二個 Print 語句打印解釋器默認值。

常用參數(shù)項

最后,對上述函數(shù)常用的參數(shù)項做以下總結(jié):

參數(shù) 說明
display.max_rows 最大顯示行數(shù),超過該值用省略號代替,為None時顯示所有行。
display.max_columns 最大顯示列數(shù),超過該值用省略號代替,為None時顯示所有列。
display.expand_frame_repr 輸出數(shù)據(jù)寬度超過設(shè)置寬度時,表示是否對其要折疊,F(xiàn)alse不折疊,True要折疊。
display.max_colwidth 單列數(shù)據(jù)寬度,以字符個數(shù)計算,超過時用省略號表示。
display.precision 設(shè)置輸出數(shù)據(jù)的小數(shù)點位數(shù)。
display.width 數(shù)據(jù)顯示區(qū)域的寬度,以總字符數(shù)計算。
display.show_dimensions 當數(shù)據(jù)量大需要以truncate(帶引號的省略方式)顯示時,該參數(shù)表示是否在最后顯示數(shù)據(jù)的維數(shù),默認 True 顯示,F(xiàn)alse 不顯示。

上述參數(shù)項,基本上可以滿足我們的日常需求。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號