2016-11-22 209 views
2

說我有一個這樣的數據幀進行分組:大熊貓:計算列的平均值由另一列

  gender  height  weight C 
2000-01-01 male 42.849980 157.500553 1 
2000-01-02 male 49.607315 177.340407 1 
2000-01-03 male 56.293531 171.524640 1 
2000-01-04 female 48.421077 144.251986 2 
2000-01-05 male 46.556882 152.526206 2 
2000-01-06 female 68.448851 168.272968 1 
2000-01-07 male 70.757698 136.431469 2 
2000-01-08 female 58.909500 176.499753 3 
2000-01-09 female 76.435631 174.094104 3 
2000-01-10 male 45.306120 177.540920 2 

我怎麼能計算height列的平均值,由C列分組?這將產生3個不同的值:具有C=1的那些高度的平均值,具有C=2的那些高度的平均值等等。

到目前爲止,我試過,但無濟於事:

df['height'].mean(groupby='C')

- >返回TypeError: mean() got an unexpected keyword argument 'groupby'

+1

你是否在'df.groupby('C')['height']。mean()'?之後? – EdChum

+0

是的。如果你願意,你可以把它作爲別人知道的答案。 – FaCoffee

回答

2

你的語法不正確,沒有groupby ARG爲mean,要groupby上然後在感興趣的列上呼叫mean

In [11]: 
df.groupby('C')['height'].mean() 

Out[11]: 
C 
1 54.299919 
2 52.760444 
3 67.672566 
Name: height, dtype: float64