iT邦幫忙

2024 iThome 鐵人賽

DAY 18
0
Python

探索 Python 世界:從語法基礎到圖像魔法系列 第 18

探索 Python 世界:從語法基礎到圖像魔法 Day18

  • 分享至 

  • xImage
  •  

Pandas資料讀取、清理、分析

Pandas是什麼?

Pandas可以讀取各種檔案轉欄是式資料格式,進而過濾或資料前處理

Pandas 主要的資料結構

Series 是一維數組,類似於列表,但具有索引(標籤)功能

DataFrame 是二維表格,類似於電子表格或 SQL 表格(每列可以有不同的數據類型)

Pandas 中常用的函數

Series 常用的函數

pd.Serise()建立 Serise 物件,可利用串列或字典來建立

s.ndim; s.shape; s.size查詢物件 s 的維度、形狀和元素個數

s.dtypes查詢物件 s 的元素類別

s.values提取物件 s 的值

s.index提取物件 s 的索引名稱

s.where(list)若 list 裡的元素為 True ,則提取該元素,否則回傳 NaN

s.isna()判別 s 的元素是否缺失值

s.notna()判別 s 裡的元素是否不是缺失值

s.fillna(n)將 s 裡的缺失值填上n

s.dropna()刪除 s 裡的缺失值

DataFrame 常用的函數

pd.DataFrame()建立 DataFrame 物件,可利用串列或字典來建立

d.dtypes查詢物件 d 的類別

d.index提取或設定物件 d 的列索引

d.columns提取或設定物件 d 的欄索引

d.head(n)提取物件 d 的前 n 筆資料

d.tail(n)提取物件 d 的後 n 筆資料

d.T將物件 d 轉置,也就是欄與列互換

Serise 與 DataFrame 皆可使用的函數

d.reindex(new_list)將物件 d 的列索引依 new_list 重新排列

d.drop(n)刪除物件 d 索引為 n 的元素

d1.append(d2)將資料 d2 附加在 d1 之後

d.loc[row,col]依列索引 row 和欄索引 col 來提取元素

d.iloc[row,col]依列和欄索引來提取元素,這裡的 (row, col)為整數

d1.add(d2)同 d1+d2

d1.sub(d2)同 d1-d2

d1.mul(d2)同 d1*d2

d1.div(d2)同 d1/d2

d.index.duplicated()判別物件 d 的列索引是否重複

實作範例

Series

import pandas as pd

# 建立一個 Series 物件,利用串列建立並檢視
s = pd.Series([1, 2, None, 4, 5])
print("Series 物件:\n", s)

print("維度:", s.ndim) # 查詢物件 s 的維度

print("形狀:", s.shape)  # 查詢物件 s 的形狀

print("元素個數:", s.size)  # 查詢物件 s 的元素個數

print("元素類別:", s.dtypes)  # 查詢物件 s 的元素類別

print("物件的值:", s.values)  # 提取物件 s 的值

print("索引名稱:", s.index)  # 提取物件的索引名稱

print("使用 where 後:\n", s.where(list_condition)) # 若 list 裡的元素為 True,則提取該元素,否則回傳 NaN

print("元素是否缺失值:\n", s.isna()) # 判別 s 的元素是否缺失值

print("元素是否不是缺失值:\n", s.notna()) # 判別 s 裡的元素是否不是缺失值

# 將 s 裡的缺失值填上 0
s_filled = s.fillna(0)
print("填上缺失值後的 Series:\n", s_filled)

# 刪除 s 裡的缺失值
s_dropped = s.dropna()
print("刪除缺失值後的 Series:\n", s_dropped)

將程式導入Visual Studio Code執行:
https://ithelp.ithome.com.tw/upload/images/20240926/20168687pOmhUDvqJH.png
https://ithelp.ithome.com.tw/upload/images/20240926/20168687gcjN8WluSf.png

DataFrame

import pandas as pd

# 建立一個 DataFrame 物件,利用串列建立並檢視
data = {
    'A': [1, 2, 3, 4, 5],
    'B': [5, 4, None, 2, 1],
    'C': ['a', 'b', 'c', 'd', 'e']
}
df = pd.DataFrame(data)
print("DataFrame 物件:\n", df) 

print("元素類別:\n", df.dtypes) # 查詢物件 d 的類別

print("列索引:\n", df.index) # 提取或設定物件 d 的列索引

print("欄索引:\n", df.columns) # 提取或設定物件 d 的欄索引

print("前 3 筆資料:\n", df.head(3)) # 提取物件 d 的前 n 筆資料

print("後 2 筆資料:\n", df.tail(2)) # 提取物件 d 的後 n 筆資料

print("轉置:\n", df.T) # 將物件 d 轉置

將程式導入Visual Studio Code執行:
https://ithelp.ithome.com.tw/upload/images/20240926/20168687CWPJEmxbHo.png

參考資料:
https://medium.com/seaniap/pandas%E5%9F%BA%E7%A4%8E%E4%BB%8B%E7%B4%B9-%E9%80%B2%E5%85%A5%E8%B3%87%E6%96%99%E7%A7%91%E5%AD%B8%E7%9A%84%E9%A0%98%E5%9F%9F-be9894b3548

https://bbs.huaweicloud.com/blogs/281723


上一篇
探索 Python 世界:從語法基礎到圖像魔法 Day17
下一篇
探索 Python 世界:從語法基礎到圖像魔法 Day19
系列文
探索 Python 世界:從語法基礎到圖像魔法20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言