Python | Pandas套件基本使用

Kombayar
Nov 12, 2020

--

Pandas 為 panel data 衍生而來,提供高效能、簡易使用的資料格式,使用者可以快速操作集分析資料。主要特色為,在異質數據的讀取、轉換與處理上,有更輕易的處理方式;提供兩種資料結構,Series用於處理時間序列相關資料,建立一維陣列;DataFrame用於處理結構化資料,有列索引與欄標籤的二維資料。與更多的輸出輸入整合性。

1. Pandas相關指令

A. 載入Pandas套件: import pandas as pd

B. 查詢目前使用版本: pd.__version__

C. 查閱套件內容: np.info(pd)/help(pd)

D. 查詢套件中某一函數: pd.lookfor(‘function_name’)

2. Pandas 資料結構

I. Series

A. 建立Series: .Series(data)

分為陣列建立與字典建立兩種方法

B. 資料選擇與篩選 [key]

C. 資料切片[:]

II. DataFrame

A. 建立DataFrame: .DataFrame()

分為陣列建立與字典建立兩種方法

B. 資料查詢

.shape: 列數與欄數; .describe(): 統計資料;.head(n): 前n筆資料; .taik(n): 後n筆資料; .columns: 欄位名稱; .index: 列資料; .info(): 資料內容。

C. 資料選擇與查詢 []

.loc[]: 先行後列,使用標籤查詢;iloc[]: 先行後列,使用索引值查詢。

D. 資料排序

.sort_index(): 運用索引值做排序,axist指定第幾欄,ascending設定升冪或降冪;.sort_values(): 運用欄位數值做排序。

3. Pandas 讀取外部資料

A. 讀取文字檔 .txt或.csv

.read_csv(path)

B. 讀取EXCEL檔案

.read_excel(path)

C. 讀取HTML檔案

.read_html(“URL”)

4. Pandas 資料處理

空值對後續資料處理匯產上許多問題,在資料處理之前須依序補填各空值欄位,以利資料處理。

A. 判斷空值

.isnull(): 判斷是否為空值;notnull(): 畔對是否不是為空值。

B. 處理空值

.dropna(): 將空值欄丟棄;.fillna(x)/({}): 將空值欄填入x值。

參考維基百科與《Python 程式設計學習經典》

--

--

Kombayar
Kombayar

Written by Kombayar

I’m a graduate student. Self-teaching coding on Internet, wanted to write some articles to make myself remember the code.

No responses yet