透過
pandas
的使用,我們經常要互動式地展示表格(
dataframe
)、分析表格。而表格的格式就顯得尤為重要了,因為大部分時候如果我們直接展示表格,格式並不是很友好。
其實呢,這些痛點都可以透過
pandas
的
option
來解決。短短几行程式碼,只要提前配置好,一次設定好,全域性生效,perfect!
# 使用方法import pandas as pdpd。set_option()pd。get_option()# 使用屬性,例如展示的最大行數pd。option。display。max_rows
東哥整理了8個常用的配置選項,供大家參考。記住這8個option程式碼,下次直接貼上進去,效率可以提高很多,爽歪歪。
顯示更多行
顯示更多列
改變列寬
設定float列的精度
數字格式化顯示
更改繪圖方法
配置info()的輸出
打印出當前設定並重置所有選項
1。 顯示更多行
預設情況下,
pandas
是不超出螢幕的顯示範圍的,如果表的行數很多,它會截斷中間的行只顯示一部分。我們可以透過設定
display。max_rows
來控制顯示的最大行數,比如我想設定顯示200行。
pd。set_option(‘display。max_rows’, 200)# pd。options。display。max_rows = 200
如果行數超過了
display。max_rows
,那麼
display。min_rows
將確定顯示的部分有多少行。因為
display。min_rows
的預設行數為5,,下面例子只顯示前5行和最後5行,中間的所有行省略。
同理,也可根據自己的習慣顯示可顯示的行數,比如10, 20。。
pd。set_option(‘display。min_rows’, 10)# pd。options。display。min_rows = 10
還可以直接重置。
# 重置pd。reset_option(‘display。max_rows’)
2。 顯示更多列
行可以設定,同樣的列也可以設定,
display。max_columns
控制著可顯示的列數,預設值為20。
pd。get_option(‘display。max_columns’) # pd。options。display。max_columns20
3。 改變列寬
pandas
對列中顯示的字元數有一些限制,預設值為50字元。所以,有的值字元過長就會顯示省略號。如果想全部顯示,可以設定
display。max_colwidth
,比如設定成500。
pd。set_option (‘display。max_colwidth’,500)# pd。options。display。max_colwidth = 500
4。 設定float列的精度
對於float浮點型資料,
pandas
預設情況下只顯示小數點後6位。我們可以透過預先設定
display。precision
讓其只顯示2位,避免後面重複操作。
pd。set_option( ‘display。precision’,2)# pd。options。display。precision = 2
這個設定不影響底層資料,它隻影響浮動列的顯示。
5。 數字格式化顯示
pandas
中有一個選項
display。float_formatoption
可以用來格式化任何浮點列。這個僅適用於浮點列,對於其他資料型別,必須將它們轉換為浮點數才可以。
用逗號格式化大值數字
例如 1200000 這樣的大數字看起來很不方便,所以我們用逗號進行分隔。
pd。set_option(‘display。float_format’,‘{:,}’。format)
設定數字精度
和上面
display。precision
有點類似,假如我們只關心小數點後的2位數字,我們可以這樣設定格式化:
pd。set_option(‘display。float_format’, ‘{:,。2f}’。format)
百分號格式化
如果我們要顯示一個百分比的列,可以這樣設定。
pd。set_option(‘display。float_format’, ‘{:。2f}%’。format)
或者其它幣種的符號等均可,只需要在大括號
{}
前後新增即可。
6。 更改繪圖方法
預設情況下,
pandas
使用
matplotlib
作為繪圖後端。從 0。25 版本開始,
pandas
提供了使用不同後端選擇,比如
plotly
,
bokeh
等第三方庫,但前提是你需要先安裝起來。
這個東哥之前也分享過設定後端視覺化方法的內容:再見,視覺化!你好,pandas!
設定很簡單,只要安裝好三方庫後,同樣只需要一行。
import pandas as pdimport numpy as nppd。set_option(‘plotting。backend’, ‘altair’)data = pd。Series(np。random。randn(100)。cumsum())data。plot()
7。 配置info()的輸出
pandas
中我們經常要使用
info()
來快速檢視
DataFrame
的資料情況。但是,
info
這個方法對要分析的最大列數是有預設限制的,並且如果資料集中有
null
,那麼在大資料集計數統計時會非常慢。
pandas
提供了兩種選擇:
display。max_info_columns
: 設定要分析的最大列數,預設為100。
display。max_info_rows
: 設定計數null時的閾值,預設為1690785。
比如,在分析有 150 個特徵的資料集時,我們可以設定
display。max_info_columns
為涵蓋所有列的值,比如將其設定為 200:
pd。set_option(‘display。max_info_columns’, 200)
在分析大型資料集時,df。info()由於要計算所有
null
,導致速度很慢。因此我們可以簡單地設定
display。max_info_rows
為一個小的值來避免計數,例如只在行數不超過5時才計數
null
:
pd。set_option(‘display。max_info_rows’, 5)
8。 打印出當前設定並重置所有選項
pd。describe_option()
將打印出設定的描述及其當前值。
pd。describe_option()
還可以列印特定的選項,例如,行顯示。
# 具體的搜尋pd。describe_option(‘rows’)
最後,我們還可以直接全部重置。
pd。reset_option(‘all’)
以上就是8個常用
set_option
的使用,下面進行了彙總,方便大家貼上使用。
pd。set_option(‘display。max_rows’,xxx) # 最大行數pd。set_option(‘display。min_rows’,xxx) # 最小顯示行數pd。set_option(‘display。max_columns’,xxx) # 最大顯示列數pd。set_option (‘display。max_colwidth’,xxx) #最大列字元數pd。set_option( ‘display。precision’,2) # 浮點型精度pd。set_option(‘display。float_format’,‘{:,}’。format) #逗號分隔數字pd。set_option(‘display。float_format’, ‘{:,。2f}’。format) #設定浮點精度pd。set_option(‘display。float_format’, ‘{:。2f}%’。format) #百分號格式化pd。set_option(‘plotting。backend’, ‘altair’) # 更改後端繪圖方式pd。set_option(‘display。max_info_columns’, 200) # info輸出最大列數pd。set_option(‘display。max_info_rows’, 5) # info計數null時的閾值pd。describe_option() #展示所有設定和描述pd。reset_option(‘all’) #重置所有設定選項
參考:
[1] https://pandas。pydata。org/pandas-docs/stable/reference/api/pandas。set_option。html
[2] https://towardsdatascience。com/8-commonly-used-pandas-display-options-you-should-know-a832365efa95
原文: https://mp。weixin。qq。com/s/t3txvsJqy1xepsFRy2xi8w