2015-10-21 121 views

回答

41

您可以使用GROUPBY的size

In [11]: df.groupby(["Group", "Size"]).size() 
Out[11]: 
Group  Size 
Moderate Medium 1 
      Small  1 
Short  Small  2 
Tall  Large  1 
dtype: int64 

In [12]: df.groupby(["Group", "Size"]).size().reset_index(name="Time") 
Out[12]: 
     Group Size Time 
0 Moderate Medium  1 
1 Moderate Small  1 
2  Short Small  2 
3  Tall Large  1 
+0

感謝。根據頻率(「時間」)選擇前k(= 20)個值的小增加值:df.groupby([「Group」,「Size」])。size()。reset_index(name =「Time」) .sort_values(由= '時間',升序=假)。頭(20); –

10

您也可以嘗試pd.crosstab()

Group   Size 

Short   Small 
Short   Small 
Moderate  Medium 
Moderate  Small 
Tall   Large 

pd.crosstab(df.Group,df.Size) 


Size  Large Medium Small 
Group       
Moderate  0  1  1 
Short   0  0  2 
Tall   1  0  0 

編輯:爲了讓你出把

pd.crosstab(df.Group,df.Size).replace(0,np.nan).\ 
    stack().reset_index().rename(columns={0:'Time'}) 
Out[591]: 
     Group Size Time 
0 Moderate Medium 1.0 
1 Moderate Small 1.0 
2  Short Small 2.0 
3  Tall Large 1.0 
+1

不錯。你甚至可以添加'邊距=真'來獲得邊際計數! –

相關問題