0

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.

2 Answers 2

2

Just add both columns and then groupby:

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

major
Art         23
Business    17
Engineer    26
dtype: int64
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.