認識Pandas Series|Python Pandas中的「單維度資料」【Python練習Day16】

Pandas Series用法是Python Pandas中非常基礎、但是重要的觀念,這篇筆記主要是Pandas Series入門,並且練習一些Pandas中的Series用法。

By.

min read

crop cyber spy hacking system while typing on laptop

pexels-photo-5935794

Pandas Series用法是Python Pandas中非常基礎、但是重要的觀念,這篇筆記主要是Pandas Series入門,並且練習一些Pandas中的Series用法。

認識Pandas Series|Python Pandas中的「單維度資料」【Python練習Day16】

哪些是常見的Pandas Series用法

關於Pandas套件中內建的模組功能,Series的常見用法,在本文中有以下的介紹和練習。
1.資料索引
2.觀察資料型態
3.取得特定資料
4.數字運算
5.字串運算


安裝Pandas套件

如果是第一次使用Pandas,必須先安裝套件,如果想了解更多Pandads基礎概念,可以參考上一篇<學習Python Pandas基礎概念,Python資料分析入門第1課【Python練習Day15】>。

!pip install pandas

安裝完成

Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (1.1.5)
Requirement already satisfied: numpy>=1.15.4 in /usr/local/lib/python3.6/dist-packages (from pandas) (1.19.4)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.6/dist-packages (from pandas) (2.8.1)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas) (2018.9)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.7.3->pandas) (1.15.0)

Series用法:資料索引

索引是資料的獨立編號(index),在Pandas中,如果沒有定義索引,預設是由數字作為索引直,如果需要自訂索引,可以使用pd.Series(資料列表,index=索引列表)的方式創建。

練習:創建一個Series,索引值為a.b.c.d

data=pd.Series([5,4,-2,3,7],index=["a","b","c","d","e"])

執行結果

a    5
b    4
c   -2
d    3
e    7
dtype: int64

Series用法:觀察資料

利用套件語法,回傳資料型態(dtype)、資料數量(size)、資料索引(index)。

練習:

print("資料型態",data.dtype)
print("資料數量",data.size)
print("資料索引",data.index)

執行結果

資料型態 int64
資料數量 5
資料索引 Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

Series用法:取得特定資料

根據順序取值[];根據所引取值[索引]。

練習:取得第三個值,第三個索引值。

data=pd.Series([5,4,-2,3,7],index=["a","b","c","d","e"])

print(data[2])
print(data.index[2])

執行結果

-2
c

Series用法:數字運算

Pandas內建的功能,可以將Series中的數字資料直接做運算,常見的包含sum(加總)、max(回傳最大值)、prod(乘法總和)、mean(平均數)、median(中位數)、std(標準差)、nlargest(取X大數字)、nsmallest(取X小數字)。

練習:

data_2=pd.Series([5,4,-2,3,7],index=["a","b","c","d","e"])
print("最大值",data_2.max())
print("總和",data_2.sum())
print("標準差",data_2.std())
print("中位數",data_2.median())
print("最大的三個數",data_2.nlargest(3))
print("最小的三個數",data_2.nsmallest(3))

執行結果

最大值 7
總和 17
標準差 3.361547262794322
中位數 4.0
最大的三個數 e    7
a    5
b    4
dtype: int64
最小的三個數 c   -2
d    3
b    4
dtype: int64

Series用法:字串運算

而針對Series中的字串,也有一些基本的運算能使用,包括:lower(把字變小寫)、upper、len(計算字串長度)、cat(串接資料)、contains(判斷是否包含)、replace(取代運算)等等。

練習:

#字串運算:基本、串接、搜尋、取代
data_str=pd.Series(["您好","Python","Pandas"])
print("字串變小寫:\n",data_str.str.lower())
print("------")
print("字串長度:\n",data_str.str.len())
print("------")
print("字串串接:\n",data_str.str.cat(sep="-"))
print("------")
print("搜尋是否包含P:\n",data_str.str.contains("P"))
print("------")
print("將您好取代Hello:\n",data_str.str.replace("您好","Hello"))

執行結果

字串變小寫:
0    您好
1    python
2    pandas
dtype: object
------
字串長度:
0    2
1    6
2    6
dtype: int64
------
字串串接:
您好-Python-Pandas
------
搜尋是否包含P:
0    False
1     True
2     True
dtype: bool
------
將您好取代Hello:
0     Hello
1    Python
2    Pandas
dtype: object

本次練習檔與影片

練習檔:Day16(1/8):Pandas資料分析Series 單維度資料
影片:Python Pandas 資料分析 – Series 單維度資料

【Python新手練習】相關文章

【Python新手練習】系列文,是一個行銷人學習Python的筆記,希望可以藉由練習程式,訓練更強大的邏輯思考,如果你也對這塊有興趣,這些文章或許可以幫助到你!也歡迎一起交流。

行銷人的Python練習90計畫-為什麼要學Python?
Day0:Google Colab新手Python開發環境推薦
Day1:Python變數與資料型態
Day2:Python判斷式與迴圈(流程控制)
Day3:Python函式 定義、呼叫與回傳
Day4:載入Python模組與使用
Day5:Python檔案讀取和儲存
Day6:Python亂數與統計
Day7:Python API 開放資料串接,抓取台北市政府公開資料
Day8:Python實體物件建立與使用
Day9:Python爬蟲實戰!抓取PTT電影版

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *