Pandas

Pandas

Here are some tutorials and examples :

Before you start, It's important to import numpy and pandas in your code.

import numpy as np
import pandas as pd

Object creation

Series 是 Pandas 的一種 Data structure 用來呈現 one column 的 elements

pd.Series([1, 2, np.nan, 3])

# 0    1.0
# 1    2.0
# 2    NaN
# 3    3.0
# dtype: float64

DataFrame 則是 Pandas 用來呈現 2D elements 的 Data strucutre

Viewing Data

要讀取 table 的頭尾可以利用 head 和 tail 函式

另外也可以列出所有的 rows 和 columns

其他的還有查看 table 的 describe (mean, std, min, max, ...)

轉置 table,或是透過特定 axis 或特定 columns 來 sorting table

Selection

想要只看單一 column 或是一個範圍內的 data :

使用 loc 可以做很多事情

iloc 則是 loc 的另一種方式,是使用 int position 作為 parameters

以下是 boolean indexing 的方式

Settings

Missing Data

接著有一些處理 missing data (NaN) 的方法

Apply function

Histogramming

Merge

Concat 可以將 objects 組合起來

也可以用 SQL-like 的 merge (join) 方法

另一個 Merge 的 example

最後還有 append

Grouping

"group by" 可以細分成以下三個 steps

  • Splitting the data into groups based on some criteria

  • Applying a function to each group independently

  • Combining the results into a data structure

Reshaping

The stack() method “compresses” a level in the DataFrame’s columns.

The inverse operation of stack() is unstack(), which by default unstacks the last level:

Categoricals

Pandas 能夠將資料分類

也能將分類重新命名

就可以依分類做 sort 或 groupby

Plotting

Getting Data In/Out

  • 讀取 csv 檔的方法

  • 輸出 csv 檔的方法

  • Excel

Last updated

Was this helpful?