day25 計算松鼠不同顏色數量 用groupby

原本的教法是分開處理

grey_squirrels_count = len(data[data["Primary Fur Color"] == "Gray"])
red_squirrels_count = len(data[data["Primary Fur Color"] == "Cinnamon"])
black_squirrels_count = len(data[data["Primary Fur Color"] == "Black"])

print(data[data["Primary Fur Color"] == "Gray"]) 
# 會抓出符合條件的一列資料(包含30 series, 全部的欄位)
# 因此要加上len去計算長度

print(grey_squirrels_count) # 2473
print(red_squirrels_count) # 392
print(black_squirrels_count) #103

data_dict = {
    "Fur Color": ["Gray", "Cinnamon", "Black"],
    "Count": [grey_squirrels_count, red_squirrels_count, black_squirrels_count]
}

result = pd.DataFrame(data_dict)
print(result)

結果
Fur Color Count
0 Gray 2473
1 Cinnamon 392
2 Black 103

但可以用groupby的方式整理後 用count 計算

import pandas as pd
data = pd.read_csv("2018_Central_Park_Squirrel_Census_-_Squirrel_Data.csv")
# 先用groupby 聚合後 只選出Primary Fur Color欄位的資料 再計算總數
result = data.groupby("Primary Fur Color")["Primary Fur Color"].count()
print(result)

結果
Primary Fur Color
Black 103
Cinnamon 392
Gray 2473

1 Like