Pandas設(shè)置數(shù)據(jù)顯示格式實(shí)現(xiàn)方式
Pandas設(shè)置數(shù)據(jù)顯示格式
在用 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),基本上可以滿足我們的日常需求。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python獲取網(wǎng)段內(nèi)ping通IP的方法
今天小編就為大家分享一篇Python獲取網(wǎng)段內(nèi)ping通IP的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python實(shí)現(xiàn)Canny及Hough算法代碼實(shí)例解析
這篇文章主要介紹了Python實(shí)現(xiàn)Canny與Hough算法代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
一文詳述 Python 中的 property 語(yǔ)法
這篇文章主要介紹了一文詳述 Python 中的 property 語(yǔ)法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Python使用itchat模塊實(shí)現(xiàn)簡(jiǎn)單的微信控制電腦功能示例
這篇文章主要介紹了Python使用itchat模塊實(shí)現(xiàn)簡(jiǎn)單的微信控制電腦功能,結(jié)合實(shí)例形式分析了Python基于itchat模塊控制電腦實(shí)現(xiàn)運(yùn)行程序、截圖等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
python 給DataFrame增加index行名和columns列名的實(shí)現(xiàn)方法
今天小編就為大家分享一篇python 給DataFrame增加index行名和columns列名的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Python大數(shù)據(jù)量文本文件高效解析方案代碼實(shí)現(xiàn)全過(guò)程
在數(shù)據(jù)分析中,有時(shí)數(shù)據(jù)源會(huì)是超大的文本文件(幾G,或在幾十G),需要從中提取需要的信息,下面這篇文章主要給大家介紹了關(guān)于Python大數(shù)據(jù)量文本文件高效解析方案代碼實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-12-12

