Dread
Dread

Reputation: 861

Python Average of Multiple Columns and Rows

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

Upvotes: 0

Views: 3315

Answers (2)

BENY
BENY

Reputation: 323246

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

Upvotes: 3

user2285236
user2285236

Reputation:

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

Upvotes: 2

Related Questions