1. 程式人生 > >Python 資料分析與展示筆記4 -- Pandas 庫基礎

Python 資料分析與展示筆記4 -- Pandas 庫基礎

Python 資料分析與展示筆記4 – Pandas 庫基礎


Python 資料分析與展示系列筆記是筆者學習、實踐Python 資料分析與展示的相關筆記

課程連結: Python 資料分析與展示

參考文件:
Numpy 官方文件(英文)
Numpy 官方文件(中文)
PIL 官方文件
Matplotlib 官方文件
Pandas 官方文件(英文)
Pandas 官方文件(中文)
Pandas 官方文件PDF下載


一、Pandas


1、安裝、匯入 Pandas

# 安裝
pip3 install pandas

# 匯入
import pandas as pd

2、Pandas 簡介

  • Pandas 是 Python 第三方庫,提供高效能易用資料型別和分析工具
  • Pandas 基於 NumPy 實現,提供更多樣的索引,除了自動索引,可以新增自定義索引
  • 使用索引時只能使用自動索引/自定義索引,不能混合使用

二、 Pandas 資料型別


1、Series 型別

Series是帶索引標籤的一維陣列

建立 Series 型別: 可以基於以下型別建立

  • 標量值
  • Python 列表
  • Python 字典
  • 一維 ndarray 陣列
  • 其他函式
import pandas as pd
import numpy as np

# 基於標量值建立
s = pd.Series(10, index=['a', 'b'])
print(s)

>>> a    10
	b    10
	dtype: int64

# 基於列表建立
s = pd.Series([1, 2], index=['a', 'b'])
print(s)

>>> a    1
	b    2
	dtype: int64

# 基於字典建立,index 可以從字典的鍵值選擇需要的,沒有的話用 NaN填充
s = pd.Series({'b': 1, 'a': 2, 'c': 3}, index=['a', 'b', 'd'])
print(s)

>>> a    2.0
	b    1.0
	d    NaN
	dtype: float64

# 基於 ndarray 陣列建立,index 也可以是 ndarray 陣列
s = pd.Series(np.arange(3), index=np.arange(5, 0, -2))
print(s)

>>> 5    0
	3    1
	1    2
	dtype: int32

Series 型別的基本操作:

  • Series 型別包括 index 和 values 兩部分,.index 獲得索引
    .values 獲得資料
  • Series 型別的操作類似 ndarray 型別,索引、切片、使用 numpy 函式
  • Series 型別的操作類似 Python 字典型別,in、.get()
  • Series 物件和索引都可以有一個名字,儲存在屬性.name中,可以修改
  • Series 型別在運算中會自動對齊不同索引的資料,各自沒有的索引其值賦為 NaN 再運算
import pandas as pd
import numpy as np

# 建立一個 Series 陣列
s = pd.Series([1, 2, 3], index=['a', 'b', 'c'])

# 獲得索引
s.index
>>> Index(['a', 'b', 'c'], dtype='object')

# 獲得資料
s.values
>>> [1 2 3]

# 索引訪問資料,自動索引和自定義索引並存,但兩套索引並存,但不能混用
s[['a', 'b']]
>>> a    1
	b    2

s[0]
>>> 1

# 切片
s[:2]
>>> a    1
	b    2

# numpy 運算函式
np.exp(s)
>>> a     2.718282
	b     7.389056
	c    20.085537
	dtype: float64

# in 操作
'b' in s
>>> True

# .get() 操作
s.get('d', 4)
>>> 4

# 修改Series 物件和索引名稱
print(s.name)
print(s.index.name)
s.name = 'series name'
s.index.name = 'index name'
print(s.name)
print(s.index.name)
>>> None
	None
	series name
	index name

#  Series 運算對齊索引
a = pd.Series([1, 2], ['a', 'b'])
b = pd.Series([2, 3], ['b', 'd'])
a + b
>>> a    NaN
	b    4.0
	d    NaN
	dtype: float64

2、DataFrame

DataFrame 是一個表格型的資料型別,每列值型別可以不同,既有行索引、也有列索引,常用於表達二維資料,但可以表達多維資料

建立 Series 型別: 可以基於以下型別建立

  • 二維 ndarray 物件
  • 由一維 ndarray、列表、字典、元組或 Series 構成的字典
  • Series 型別
  • 其他的 DataFrame 型別
import pandas as pd
import numpy as np

# 基於二維 ndarray 物件建立
d = pd.DataFrame(np.arange(4).reshape(2, 2))
print(d)
>>>    0  1
	0  0  1
	1  2  3

# 基於 Series 字典
d = pd.DataFrame({'one': pd.Series([1, 2], ['a', 'b']),
                  'two': pd.Series([3, 4], ['a', 'b'])})
print(d)
>>>    one  two
	a    1    3
	b    2    4

# 基於列表型別的字典建立
d = pd.DataFrame({'one': [1, 2], 'two': [3, 4]})
print(d)
>>>    one  two
	0    1    3
	1    2    4

DataFrame 索引型別的基本操作:

方法 說明
.reindex() 改變或重排Series和DataFrame索引
.drop() 刪除Series和DataFrame指定行或列索引
.delete(loc) 刪除loc位置處的元素
.insert(loc,e) 在loc位置增加一個元素e
.append(idx) 連線另一個Index物件,產生新的Index物件
.diff(idx) 計算差集,產生新的Index物件
.intersection(idx) 計算交集
.union(idx) 計算並集

DataFrame 算術運算的基本操作:

方法 說明
.add(d, **argws) 加法運算,+
.sub(d, **argws) 減法運算,-
.mul(d, **argws) 乘法運算,*
.div(d, **argws) 除法運算,/

GOOD LUCK!