简体   繁体   中英

Sum based on grouping in pandas dataframe?

I have a pandas dataframe df which contains:

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.

I have tried

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

But this separately sums the men and women rather than combining their totals.

melt() then 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 :

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

major
Art         23
Business    17
Engineer    26
dtype: int64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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