我一直在对数据框执行分组操作,该数据框基于“名称”列将各列聚合在一起:

Name | As | Bs | Cs   |  Note
Mark   3     4     7     Good
Luke   2     1     12    Well
Mark   5     6     8     Ok
John   1     18    3     Great


因此,在这种情况下,使用以下代码将带有“ Mark”的行汇总到A,B和C列上:

temp_df = temp_df.groupby(['Name'], as_index=False).agg({'As': np.sum, 'Bs': np.sum,'Cs': np.sum})


我需要添加的一件事是对“名称”中具有相同值的行数进行计数。这将给我类似以下的输出:

Name | As | Bs | Cs   |  Note   | Count
Mark   8     10    15    Good      2
Luke   2     1     12    Well      1
John   1     18    3     Great     1


如何修改上面的代码行以完成所需的工作?

最佳答案

创建组并进行聚合:

the_group = temp_df.groupby(['Name'], as_index=False)
temp_df = the_group.agg({'As': np.sum, 'Bs': np.sum,'Cs': np.sum})


然后从size计算the_group

temp_df['count'] = the_group.count()['Note']


给出:

   Name  Cs  As  Bs  count
0  John   3   1  18      1
1  Luke  12   2   1      1
2  Mark  15   8  10      2


编辑:

如注释中所建议,如果数据包含size(),则使用NaN更安全:

temp_df['count'] = the_group.size().reset_index()[0]

09-27 11:06
查看更多