反派的 BMI
回到 heroes 資料集,裡面包含各種漫畫英雄的資訊。我們已經在資料集中新增了 bmi 欄位,計算方式為 Weight 除以 (Height/100)**2。這個指標可用來判斷個人是否有體重方面的問題。
你的任務是根據角色的 'Alignment' 和所屬的 'Publisher',找出 BMI 指標的平均值與標準差。不過,你只需要考慮那些擁有超過 10 筆有效 BMI 觀測值的群組。
小提醒:使用 .count() 來計算有效觀測值的數量。
本練習屬於課程
Python 程式面試題實作練習
練習說明
- 依照上面指定的兩個欄位進行分組。
- 篩選出擁有超過 10 筆有效
bmi觀測值的群組。 - 針對篩選後的資料,再以相同欄位進行分組。
- 計算 BMI 指標的平均值與標準差。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
import numpy as np
# Group the data by two factors specified in the context
groups = ____
# Filter groups having more than 10 valid bmi observations
fheroes = ____
# Group the filtered data again by the same factors
fgroups = ____
# Calculate the mean and standard deviation of the BMI index
result = fgroups[____].____
print(result)