简体   繁体   English

基于熊猫数据帧中分组的总和?

[英]Sum based on grouping in pandas dataframe?

I have a pandas dataframe df which contains: 我有一个熊猫数据框df,其中包含:

major       men        women        rank

Art         5          4            1
Art         3          5            3
Art         2          4            2
Engineer    7          8            3
Engineer    7          4            4
Business    5          5            4
Business    3          4            2

Basically I am needing to find the total number of students including both men and women as one per major regardless of the rank column. 基本上,我需要找到每个专业的男女学生总数,而不考虑排名列。 So for Art for example, the total should be all men + women totaling 23, Engineer 26, Business 17. 例如,对于Art来说,总数应该是23位男性和女性,工程师26位,业务17位。

I have tried 我努力了

df.groupby(['major_category']).sum()

But this separately sums the men and women rather than combining their totals. 但这是将男女分开汇总,而不是合计总数。

melt() then groupby() : melt()然后groupby()

df.drop('rank',1).melt('major').groupby('major',as_index=False).sum()

      major  value
0       Art     23
1  Business     17
2  Engineer     26

Just add both columns and then groupby : 只需添加两列,然后添加groupby

(df.men+df.women).groupby(df.major).sum()

major
Art         23
Business    17
Engineer    26
dtype: int64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM