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

2022-07-13 11:54 更新

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

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

它們的功能介紹如下:

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

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

get_option()

該函數(shù)接受單一參數(shù),用來(lái)獲取顯示上限的行數(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

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

set_option()

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

1) 修改默認(rèn)行數(shù)

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

輸出結(jié)果:

70

2) 修改默認(rèn)列數(shù)

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

輸出結(jié)果:

40

reset_option()

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

import pandas as pd
pd.reset_option("display.max_rows")
#恢復(fù)為默認(rèn)值
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í)設(shè)置 with 語(yǔ)句塊中的默認(rèn)顯示參數(shù)。當(dāng)您退出 with 語(yǔ)句塊時(shí),參數(shù)值會(huì)自動(dòng)恢復(fù)。示例如下:

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

注意:第一個(gè) Print 語(yǔ)句打印 option_context() 設(shè)置的臨時(shí)值。當(dāng)退出 with 語(yǔ)句塊時(shí),第二個(gè) Print 語(yǔ)句打印解釋器默認(rèn)值。

常用參數(shù)項(xiàng)

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

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

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

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)