Pandas設(shè)置數(shù)據(jù)顯示格式實現(xiàn)方式
Pandas設(shè)置數(shù)據(jù)顯示格式
在用 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ù),當退出使用的語句塊時,恢復(fù)為默認值。 |
下面對上述函數(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")
#恢復(fù)為默認值
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ù)值會自動恢復(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
注意:第一個 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ù)項,基本上可以滿足我們的日常需求。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python中如何使用xml.dom.minidom模塊讀取解析xml文件
xml.dom.minidom模塊應(yīng)該是內(nèi)置模塊不用下載安裝,本文給大家介紹python中如何使用xml.dom.minidom模塊讀取解析xml文件,感興趣的朋友一起看看吧2023-10-10
python文件讀取和導(dǎo)包的絕對路徑、相對路徑詳解
每次在讀或?qū)懳募r,獲取腳本文件的絕對路徑和相對路徑時很容易搞混,下面這篇文章主要給大家介紹了關(guān)于python文件讀取和導(dǎo)包的絕對路徑、相對路徑的相關(guān)資料,需要的朋友可以參考下2022-04-04
Python中函數(shù)的創(chuàng)建及調(diào)用
這篇文章主要介紹了Python中函數(shù)的創(chuàng)建及調(diào)用,創(chuàng)建函數(shù)也稱為定義一個函數(shù),可以理解為創(chuàng)建一個具有某種用途的工具。調(diào)用函數(shù)也就是執(zhí)行函數(shù)。如果把創(chuàng)建的函數(shù)理解為創(chuàng)建一個具體有某種用途的工具,那么調(diào)用函數(shù)就相當于使用該工具2022-06-06
Python 給下載文件顯示進度條和下載時間的實現(xiàn)
這篇文章主要介紹了Python 給下載文件顯示進度條和下載時間的代碼,本文通過實例代碼截圖相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Python descriptor(描述符)的實現(xiàn)
這篇文章主要介紹了Python descriptor(描述符)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python深度學(xué)習(xí)之FastText實現(xiàn)文本分類詳解
FastText是一種典型的深度學(xué)習(xí)詞向量的表示方法,它非常簡單通過Embedding層將單詞映射到稠密空間,然后將句子中所有的單詞在Embedding空間中進行平均,進而完成分類操作2022-09-09

