
- 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 - I/O 工具
- Python Pandas - I/O 工具
- 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 - 使用 HDF5 格式
在處理大型資料集時,我們可能會遇到“記憶體不足”錯誤。可以使用 HDF5 等最佳化的儲存格式來避免此類問題。pandas 庫提供了諸如 HDFStore 類和 讀/寫 API 等工具,以便輕鬆地儲存、檢索和操作資料,同時最佳化記憶體使用和檢索速度。
HDF5 代表 分層資料格式版本 5,是一種開原始檔格式,旨在有效地儲存大型、複雜和異構資料。它以類似於檔案系統的分層結構組織資料,其中組充當目錄,資料集充當檔案。HDF5 檔案格式可以以分層結構儲存不同型別的資料(例如陣列、影像、表格和文件),使其成為管理異構資料的理想選擇。
使用 HDFStore 處理 HDF5 格式
pandas 中的 HDFStore 類用於以類似字典的方式管理 HDF5 檔案。HDFStore 是一個類似字典的物件,可以使用 PyTables 庫以 HDF5 格式讀取和寫入 Pandas 資料。
示例:在 Pandas 中使用 HDFStore 建立 HDF5 檔案
以下是一個演示如何在 Pandas 中使用 pandas.HDFStore 類建立 HDF5 檔案 的示例。
import pandas as pd import numpy as np # Create the store using the HDFStore class store = pd.HDFStore("store.h5") # Display the store print(store) # It is important to close the store after use store.close()
以下是上述程式碼的輸出 -
<class 'pandas.io.pytables.HDFStore'> File path: store.h5
注意:要在 pandas 中使用 HDF5 格式,您需要 pytables 庫。它是 pandas 的可選依賴項,必須使用以下命令之一單獨安裝 -
# Using pip pip install tables # or using conda installer conda install pytables
示例:使用 Pandas 中的 HDFStore 將資料寫入/讀取到 HDF5
HDFStore 是一個類似字典的物件,因此我們可以使用鍵值對直接將資料寫入和讀取到 HDF5 儲存中。以下示例演示了相同的功能 -
import pandas as pd import numpy as np # Create the store store = pd.HDFStore("store.h5") # Create the data index = pd.date_range("1/1/2024", periods=8) s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) df = pd.DataFrame(np.random.randn(8, 3), index=index, columns=["A", "B", "C"]) # Write Pandas data to the Store, which is equivalent to store.put('s', s) store["s"] = s store["df"] = df # Read Data from the store, which is equivalent to store.get('df') from_store = store["df"] print('Retrieved Data From the HDFStore:\n',from_store) # Close the store after use store.close()
以下是上述程式碼的輸出 -
Retrieved Data From the HDFStore: A B C 2024-01-01 0.200467 0.341899 0.105715 2024-01-02 -0.379214 1.527714 0.186246 2024-01-03 -0.418122 1.008820 1.331104 2024-01-04 0.146418 0.587433 -0.750389 2024-01-05 -0.556524 -0.551443 -0.161225 2024-01-06 -0.214145 -0.722693 0.072083 2024-01-07 0.631878 -0.521474 -0.769847 2024-01-08 -0.361999 0.435252 1.177110
使用 Pandas API 讀取和寫入 HDF5 格式
Pandas 還提供了高階 API 來簡化與 HDFStore(也就是 HDF5 檔案)的互動。這些 API 允許您直接讀取和寫入 HDF5 檔案的資料,而無需手動建立 HDFStore 物件。以下是 pandas 處理 HDF5 檔案的主要 API -
pandas.read_hdf():從 HDFStore 讀取資料。
pandas.DataFrame.to_hdf() 或 pandas.Series.to_hdf():使用 HDFStore 將 Pandas 物件資料寫入 HDF5 檔案。
使用 to_hdf() 將 Pandas 資料寫入 HDF5
to_hdf() 函式允許您使用 HDFStore 將 pandas 物件(如 DataFrame 和 Series)直接寫入 HDF5 檔案。此函式提供了各種可選引數,例如壓縮、處理缺失值、格式選項等,允許您有效地儲存資料。
示例
此示例使用 DataFrame.to_hdf() 函式將資料寫入 HDF5 檔案。
import pandas as pd import numpy as np # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]},index=['x', 'y', 'z']) # Write data to an HDF5 file using the to_hdf() df.to_hdf("data_store.h5", key="df", mode="w", format="table") print("Data successfully written to HDF5 file")
以下是上述程式碼的輸出 -
Data successfully written to HDF5 file
使用 read_hdf() 從 HDF5 讀取資料
pandas.read_hdf() 方法用於檢索儲存在 HDF5 檔案中的 Pandas 物件。它接受要從中讀取資料的檔名、檔案路徑或緩衝區。
示例
此示例演示瞭如何使用 pd.read_hdf() 方法從 HDF5 檔案“data_store.h5”中鍵“df”下儲存的資料。
import pandas as pd # Read data from the HDF5 file using the read_hdf() retrieved_df = pd.read_hdf("data_store.h5", key="df") # Display the retrieved data print("Retrieved Data:\n", retrieved_df.head())
以下是上述程式碼的輸出 -
Retrieved Data: A B x 1 4 y 2 5 z 3 6
使用 to_hdf() 將資料追加到 HDF5 檔案
可以透過使用 to_hdf() 函式的 mode="a" 選項將資料追加到現有的 HDF5 檔案。當您想要將新資料新增到檔案而不覆蓋現有內容時,這很有用。
示例
此示例演示瞭如何使用 to_hdf() 函式將資料追加到現有 HDF5 檔案。
import pandas as pd import numpy as np # Create a DataFrame to append df_new = pd.DataFrame({'A': [7, 8], 'B': [1, 1]},index=['i', 'j']) # Append the new data to the existing HDF5 file df_new.to_hdf("data_store.h5", key="df", mode="a", format="table", append=True) print("Data successfully appended") # Now read data from the HDF5 file using the read_hdf() retrieved_df = pd.read_hdf("data_store.h5", key='df') # Display the retrieved data print("Retrieved Data:\n", retrieved_df.head())
以下是上述程式碼的輸出 -
Data successfully appended Retrieved Data: A B x 1 4 y 2 5 z 3 6 i 7 1 j 8 1