0

How do I group by two columns in a dataframe and specify other columns for which I want an overall average?

Data

name     team   a   b   c   d 
Bob      blue   2   4   3   5
Bob      blue   2   4   3   4
Bob      blue   1   5   3   4
Bob      green  1   3   2   5
Bob      green  1   2   1   1
Bob      green  1   2   1   4
Bob      green  5   2   2   1
Jane     red    1   2   2   3
Jane     red    3   3   3   4
Jane     red    2   5   1   2
Jane     red    4   5   5   3

Desired Output

name    team    avg
Bob     blue    3.333333333
Bob     green   2.125
Jane    red     3

2 Answers 2

3

You can mean two times :-)

df.groupby(['name','team']).mean().mean(1)
Out[1263]: 
name  team 
Bob   blue     3.333333
      green    2.125000
Jane  red      3.000000
dtype: float64
Sign up to request clarification or add additional context in comments.

Comments

2

You need to set the index as the grouping columns and stack the remaining columns:

df.set_index(['name', 'team']).stack().groupby(level=[0, 1]).mean()
Out: 
name  team 
Bob   blue     3.333333
      green    2.125000
Jane  red      3.000000
dtype: float64

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.