今天的影片內容為介紹具有強大功能的Pandas模組(對...熊貓模組)
利用這個模組,可以很方便的執行數據處理與分析~是個好幫手呢!
要執行這個外部模組前,必須至CMD進行安裝
pip install pandas
以下為影片中有使用到的程式碼
#使用串列建立Series物件
import pandas as pd
series_1 = pd.Series([1, 2, 3, 4, 5])
print(series_1)
#修改內容
series_1[1] = 10
print(series_1)
#使用字典建立Series物件
dictionary = {'動物' : '水母', '重量' : 10, '年齡' : 1}
series_2 = pd.Series(dictionary)
print(series_2)
#自行建立索引
import pandas as pd
price = [1000, 2000, 3000, 4000, 5000]
size = [100, 200, 300, 400, 500]
series_3 = pd.Series(price, index = size)
print(series_3)
weight = [100, 10, 150]
animals = ['Lion', 'Jellyfish', 'Monkey']
series_4 = pd.Series(weight, index = animals)
print(series_4)
#搜尋功能
print(series_4['Lion'])
print(series_4['Jellyfish'])
print(series_4['Monkey'])
print(series_4.values)
print(series_4.index)
#切片觀念
import pandas as pd
series_1 = pd.Series([0, 1, 2, 3, 4, 5])
print(series_1)
print()
print(series_1[1:3])
print()
print(series_1[3:])
print()
print(series_1[:3])
print()
print(series_1[-2:])
series_2 = pd.Series([1, 3, 5, 7, 9])
series_3 = pd.Series([2, 4, 6, 8, 10])
print(series_2)
print(series_3)
#四則運算
print(series_2 + series_3)
print(series_2 - series_3)
print(series_2 * series_3)
#邏輯運算
print(series_2 > series_3)
print(series_2 < series_3)
#組合Series物件成DataFrame
#axis參數一定要設定!否則無法生成DataFrame
import pandas as pd
years = range(2019, 2022) #[2019,2020,2021]
Blue_peacock = pd.Series([20, 18, 14], index = years)
Green_peacock = pd.Series([30, 25, 29], index = years)
white_peacock = pd.Series([5, 4, 2], index = years)
zoo = pd.concat([Blue_peacock, Green_peacock, white_peacock], axis = 1)
print(zoo)
#columns屬性設定
import pandas as pd
years = range(2019, 2022)
Blue_peacock = pd.Series([20, 18, 14], index = years)
Green_peacock = pd.Series([30, 25, 29], index = years)
white_peacock = pd.Series([5, 4, 2], index = years)
zoo = pd.concat([Blue_peacock, Green_peacock, white_peacock], axis = 1)
variety = ["Blue_peacock", "Green_peacock", "white_peacock"]
zoo.columns = variety
print(zoo)
#name屬性設定
import pandas as pd
years = range(2019, 2022)
Blue_peacock = pd.Series([20, 18, 14], index = years)
Green_peacock = pd.Series([30, 25, 29], index = years)
white_peacock = pd.Series([5, 4, 2], index = years)
Blue_peacock.name = "Blue_peacock"
Green_peacock.name = "Green_peacock"
white_peacock.name = "white_peacock"
zoo = pd.concat([Blue_peacock, Green_peacock, white_peacock], axis = 1)
print(zoo)
如果在影片中有說得不太清楚或錯誤的地方,歡迎留言告訴我,謝謝您的指教。