
- Python Pandas 教程
- Python Pandas - 首頁
- Python Pandas - 簡介
- Python Pandas - 環境設定
- Python Pandas - 基礎知識
- Python Pandas - 資料結構簡介
- Python Pandas - 索引物件
- Python Pandas - 面板
- Python Pandas - 基本功能
- Python Pandas - 索引和資料選擇
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - 切片 Series 物件
- Python Pandas - Series 物件的屬性
- Python Pandas - Series 物件上的算術運算
- Python Pandas - 將 Series 轉換為其他物件
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - 訪問 DataFrame
- Python Pandas - 切片 DataFrame 物件
- Python Pandas - 修改 DataFrame
- Python Pandas - 從 DataFrame 中刪除行
- Python Pandas - DataFrame 上的算術運算
- Python Pandas - IO 工具
- Python Pandas - IO 工具
- Python Pandas - 使用 CSV 格式
- Python Pandas - 讀取和寫入 JSON 檔案
- Python Pandas - 從 Excel 檔案讀取資料
- Python Pandas - 將資料寫入 Excel 檔案
- Python Pandas - 使用 HTML 資料
- Python Pandas - 剪貼簿
- Python Pandas - 使用 HDF5 格式
- Python Pandas - 與 SQL 的比較
- Python Pandas - 資料處理
- Python Pandas - 排序
- Python Pandas - 重索引
- Python Pandas - 迭代
- Python Pandas - 連線
- Python Pandas - 統計函式
- Python Pandas - 描述性統計
- Python Pandas - 使用文字資料
- Python Pandas - 函式應用
- Python Pandas - 選項和自定義
- Python Pandas - 視窗函式
- Python Pandas - 聚合
- Python Pandas - 合併/連線
- Python Pandas - 多級索引
- Python Pandas - 多級索引基礎
- Python Pandas - 使用多級索引進行索引
- Python Pandas - 使用多級索引的高階重索引
- Python Pandas - 重新命名多級索引標籤
- Python Pandas - 對多級索引進行排序
- Python Pandas - 二元運算
- Python Pandas - 二元比較運算
- Python Pandas - 布林索引
- Python Pandas - 布林掩碼
- Python Pandas - 資料重塑和透視
- Python Pandas - 透視
- Python Pandas - 堆疊和取消堆疊
- Python Pandas - 熔化
- Python Pandas - 計算虛擬變數
- Python Pandas - 分類資料
- Python Pandas - 分類資料
- Python Pandas - 分類資料的排序和排序
- Python Pandas - 比較分類資料
- Python Pandas - 處理缺失資料
- Python Pandas - 缺失資料
- Python Pandas - 填充缺失資料
- Python Pandas - 缺失值的插值
- Python Pandas - 刪除缺失資料
- Python Pandas - 使用缺失資料進行計算
- Python Pandas - 處理重複項
- Python Pandas - 重複資料
- Python Pandas - 計數和檢索唯一元素
- Python Pandas - 重複標籤
- Python Pandas - 分組和聚合
- Python Pandas - GroupBy
- Python Pandas - 時間序列資料
- Python Pandas - 日期功能
- Python Pandas - Timedelta
- Python Pandas - 稀疏資料結構
- Python Pandas - 稀疏資料
- Python Pandas - 視覺化
- Python Pandas - 視覺化
- Python Pandas - 其他概念
- Python Pandas - 注意事項和陷阱
- Python Pandas 有用資源
- Python Pandas - 快速指南
- Python Pandas - 有用資源
- Python Pandas - 討論
Python Pandas - 選項和自定義
Pandas 提供了一個 API 來自定義其行為的各個方面,尤其是在顯示設定方面。這種自定義對於根據您的需求調整資料呈現方式至關重要。無論您是想調整顯示的行數和列數,還是更改浮點數的精度,Pandas 都為這些自定義提供了靈活而強大的 API。
可用於這些自定義的主要函式有:
- get_option()
- set_option()
- reset_option()
- describe_option()
- option_context()
常用引數
在瞭解自定義選項之前,讓我們先了解一些您可以用於自定義的常用 Pandas 顯示引數:
序號 | 引數和描述 |
---|---|
1 |
display.max_rows 要顯示的最大行數。 |
2 |
2 display.max_columns 要顯示的最大列數。 |
3 |
display.expand_frame_repr 是否在多行上擴充套件 DataFrame 的顯示。 |
4 |
display.max_colwidth 列的最大寬度。 |
5 |
display.precision 顯示十進位制數字的精度。 |
現在讓我們瞭解自定義函式是如何工作的。
獲取當前選項
get_option() 函式檢索指定引數的當前值。這對於檢查 Pandas 的當前配置很有用。
示例:檢查顯示的最大行數
以下示例獲取並返回顯示的最大行數的預設值。直譯器讀取此值並以該值為顯示上限顯示行。
import pandas as pd print(pd.get_option("display.max_rows"))
其輸出如下:
60
示例:檢查顯示的最大列數
此示例返回顯示的最大列數的預設值。直譯器讀取此值並以該值為顯示上限顯示行。
import pandas as pd print(pd.get_option("display.max_columns"))
其輸出如下:
0
這裡,60 和 0 是預設配置引數值。
設定新選項
set_option() 函式允許您更改特定引數的值,使您可以自定義資料顯示方式。
示例:更改顯示的最大行數
使用set_option(),我們可以更改要顯示的行數的預設值。這是一個例子:
import pandas as pd pd.set_option("display.max_rows",10) print(pd.get_option("display.max_rows"))
其輸出如下:
10
示例:更改顯示的最大列數
以下示例使用set_option() 函式更改要顯示的列數的預設值。
import pandas as pd pd.set_option("display.max_columns",30) print(pd.get_option("display.max_columns"))
其輸出如下:
30
將選項重置為其預設值
reset_option() 函式將指定引數的值重置回其預設設定。
示例:重置顯示的最大行數
使用reset_option() 函式,我們可以將值更改回要顯示的行數的預設值。
import pandas as pd pd.reset_option("display.max_rows") print(pd.get_option("display.max_rows"))
其輸出如下:
60
描述選項
describe_option() 函式提供指定引數的描述,解釋其作用及其預設值。
示例:描述顯示的最大行數
此示例使用reset_option() 函式獲取max_row 引數的描述。
import pandas as pd pd.describe_option("display.max_rows")
其輸出如下:
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() 函式允許您在with 語句中臨時設定選項。退出上下文後,選項會自動恢復到其先前值。
示例:臨時更改顯示的最大行數
此示例使用option_context() 函式為要顯示的最大行數設定臨時值。
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"))
其輸出如下:
10 60
請注意,第一個和第二個 print 語句之間的區別。第一個語句列印由option_context() 設定的值,該值在with 上下文本身中是臨時的。在with 上下文之後,第二個 print 語句列印已配置的值。