Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: return average of multiple columns

How do you output average of multiple columns?

Gender   Age     Salary     Yr_exp   cup_coffee_daily
  Male    28    45000.0        6.0                2.0
Female    40    70000.0       15.0               10.0
Female    23    40000.0        1.0                0.0
  Male    35    55000.0       12.0                6.0

I have df.groupby('Gender', as_index=False)['Age', 'Salary', 'Yr_exp'].mean(), but it still only returned the average of the first column Age. How do you return the average of specific columns in different columns? Desired output:

Gender   Age     Salary   Yr_exp
  Male  31.5    50000.0      9.0
Female  31.5    55000.0      8.0

Thanks.

like image 353
Karma Avatar asked Mar 29 '18 16:03

Karma


1 Answers

Given this dataframe:

df = pd.DataFrame({
    "Gender": ["Male", "Female", "Female", "Male"],
    "Age": [28, 40, 23, 35],
    "Salary": [45000, 70000, 40000, 55000],
    "Yr_exp": [6, 15, 1, 12]
})

df
   Age  Gender  Salary  Yr_exp
0   28    Male   45000       6
1   40  Female   70000      15
2   23  Female   40000       1
3   35    Male   55000      12

Group by gender and use the mean() function:

df.groupby("Gender").mean()
         Age   Salary  Yr_exp
Gender                       
Female  31.5  55000.0     8.0
Male    31.5  50000.0     9.0

Edit: you may need to change the way you're indexing after groupby(): df['Age', 'Salary'] gives a KeyError, but df[['Age', 'Salary']] returns the expected:

   Age  Salary
0   28   45000
1   40   70000
2   23   40000
3   35   55000

Try changing

df.groupby("Gender", as_index=True)['Age', 'Salary', 'Yr_exp'].mean() 

to

df.groupby("Gender", as_index=True)[['Age', 'Salary', 'Yr_exp']].mean()
like image 84
Jonathan Dayton Avatar answered Nov 11 '22 03:11

Jonathan Dayton