- 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 - 從 Excel 檔案讀取資料
Pandas 庫提供了強大的資料操作和分析工具。它提供的關鍵功能之一是能夠讀取和寫入 Excel 檔案中的資料。Pandas 提供了 **read_excel()** 方法來讀取 Excel 檔案並將資料載入到 Pandas DataFrame 中。該函式支援來自本地檔案系統或 URL 的 **xls** 和 **xlsx** 副檔名,並且需要 **xlrd** 和 **openpyxl** 包才能執行。
Pandas 中支援的 Excel 檔案格式
**pandas.read_excel()** 方法可以使用不同的模組讀取各種 Excel 檔案格式 -
**Excel 2007+ (.xlsx)** 檔案可以使用 **openpyxl** Python 模組讀取。
**Excel 2003 (.xls)** 檔案可以使用 **xlrd** 模組讀取。
**二進位制 Excel (.xlsb)** 檔案可以使用 **pyxlsb** 模組讀取。
所有格式都可以使用 calamine 引擎讀取。
在本教程中,我們將學習如何使用 **pandas.read_excel()** 方法從 Excel 檔案讀取資料,涵蓋不同的場景,例如載入單個工作表、特定工作表和多個工作表。
在 Pandas 中讀取 Excel 檔案
**pandas.read_excel()** 方法用於讀取或載入 Excel 檔案到 Pandas DataFrame 中。此方法支援多種 Excel 檔案格式,例如 **.xls**、**.xlsx**、**.xlsm** 等,來自本地檔案系統或 URL。
注意:請確保您已在系統中安裝了所需的包(xlrd 和 openpyxl)。如果沒有,請使用以下命令安裝 -
pip install openpyxl或
pip install xlrd
示例
這是一個使用 **pd.read_excel()** 方法將本地系統 Excel 檔案讀取到 DataFrame 中的簡單示例,方法是指定檔案路徑。
import pandas as pd
# Read an Excel file
df = pd.read_excel('data.xlsx')
# Print the DataFrame
print('Output DataFrame:')
print(df)
以下是上述程式碼的輸出 -
Output DataFrame:
| Sr.no | Name | Gender | Age | |
|---|---|---|---|---|
| 0 | 1 | Braund | female | 38 |
| 1 | 2 | Cumings | male | 22 |
| 2 | 3 | Heikkin | female | 35 |
| 3 | 4 | Futrelle | female | 26 |
從 Excel 檔案讀取特定工作表
Excel 檔案可能包含多個具有不同名稱的工作表。要將特定工作表載入到 Pandas DataFrame 中,您可以將工作表名稱或索引指定到 **pd.read_excel()** 方法的 **sheet_name** 引數。
示例
以下是從 Excel 檔案中使用 **pd.read_excel()** 方法將特定工作表讀取到 Pandas DataFrame 中的示例。
import pandas as pd
# Read a specific sheet
df = pd.read_excel('data.xlsx', sheet_name="Sheet_2")
# Print the DataFrame
print('Output DataFrame:')
print(df)
以下是上述程式碼的輸出 -
Output DataFrame:
| Name | Value | |
|---|---|---|
| 0 | string1 | 1 |
| 1 | string2 | 3 |
| 2 | Comment | 5 |
將多個工作表讀取到 DataFrame 中
如果 Excel 檔案包含多個工作表,並且您需要將其中一些工作表讀取到 Pandas DataFrame 中,則可以透過將工作表名稱或索引列表傳遞到 **pd.read_excel()** 方法的 **sheet_name** 引數來實現。
示例
此示例使用 **pd.read_excel()** 方法將 Excel 檔案中的多個工作表讀取到 DataFrame 字典中。
import pandas as pd
# Read multiple sheets
df = pd.read_excel('data.xlsx', sheet_name=[0, 1])
# Print the DataFrame
print('Output Dict of DataFrames:')
print(df)
以下是上述程式碼的輸出 -
Output Dict of DataFrames:
{0: Sr.no Name Gender Age
0 1 Braund female 38
1 2 Cumings male 22
2 3 Heikkin female 35
3 4 Futrelle female 26, 1: Name Value
0 string1 1
1 string2 3
2 Comment 5}