10. 在使用 Pandas 進行資料分析時,下列哪一個操作能夠對資
料進行分組後計算每組的平均值?
(A) df.groupby('column').mean()
(B) df.agg('mean')
(C) df.mean('column')
(D) df.groupby('column').sum()
答案:登入後查看
統計: A(18), B(2), C(1), D(2), E(0) #3323440
統計: A(18), B(2), C(1), D(2), E(0) #3323440
詳解 (共 1 筆)
#6329499
在 Pandas 進行資料分析時,groupby() 是用來根據某一欄位分組的函式,而 mean() 則是計算每組的平均值。
選項分析
(A) df.groupby('column').mean() ✅ (正確)
- groupby('column') 會根據 "column" 的不同值進行分組。
- mean() 會對每個分組計算平均值。
- 範例:
輸出:pythonㅤㅤ
import pandas as pd
data = {'Category': ['A', 'A', 'B', 'B', 'C'], 'Value': [10, 20, 30, 40, 50]}
df = pd.DataFrame(data)
result = df.groupby('Category').mean() print(result)
ㅤㅤ→ 對 "Category" 分組後,每組計算 Value 欄位的平均值。ㅤㅤValue Category A 15.0 B 35.0 C 50.0
0
0