Warm tip: This article is reproduced from stackoverflow.com, please click
dataframe pandas python sum

Sum based on grouping in pandas dataframe?

发布于 2020-03-27 10:19:06

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.

Questioner
jwalls91
Viewed
89
yatu 2019-07-03 21:47

Just add both columns and then groupby:

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

major
Art         23
Business    17
Engineer    26
dtype: int64