料青山看我应如是
这是一个简短的示例数据框:import pandas as pddf = pd.DataFrame([['January',19],['March',6],['January',24],['November',83],['February',23], ['November',4],['February',98],['January',44],['October',47],['January',4], ['April',8],['March',21],['April',41],['June',34],['March',63]], columns=['activity_month','activity_count'])产量: activity_month activity_count0 January 191 March 62 January 243 November 834 February 235 November 46 February 987 January 448 October 479 January 410 April 811 March 2112 April 4113 June 3414 March 63如果您希望从中获得每个组的值的总和df.groupby('activity_month'),则可以这样做:df.groupby('activity_month')['activity_count'].sum()给出:activity_monthApril 49February 121January 91June 34March 90November 87October 47Name: activity_count, dtype: int64要获取与给定组相对应的行数:df.groupby('activity_month')['activity_count'].agg('count')给出:activity_monthApril 2February 2January 4June 1March 3November 2October 1Name: activity_count, dtype: int64重新阅读您的问题后,我确信您没有以最有效的方式解决此问题。我强烈建议您不要显式地遍历使用创建的轴df.hist(),特别是当此信息可以快速(直接)从其df自身访问时。