1 回答
TA贡献1772条经验 获得超8个赞
这是一个简短的示例数据框:
import pandas as pd
df = 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_count
0 January 19
1 March 6
2 January 24
3 November 83
4 February 23
5 November 4
6 February 98
7 January 44
8 October 47
9 January 4
10 April 8
11 March 21
12 April 41
13 June 34
14 March 63
如果您希望从中获得每个组的值的总和df.groupby('activity_month'),则可以这样做:
df.groupby('activity_month')['activity_count'].sum()
给出:
activity_month
April 49
February 121
January 91
June 34
March 90
November 87
October 47
Name: activity_count, dtype: int64
要获取与给定组相对应的行数:
df.groupby('activity_month')['activity_count'].agg('count')
给出:
activity_month
April 2
February 2
January 4
June 1
March 3
November 2
October 1
Name: activity_count, dtype: int64
重新阅读您的问题后,我确信您没有以最有效的方式解决此问题。我强烈建议您不要显式地遍历使用创建的轴df.hist(),特别是当此信息可以快速(直接)从其df自身访问时。
添加回答
举报