How to find mean using pandas for specific class in dataframe?

2415 views python
-4

I'm wondering if there's a straightforward way to only find the mean for a specific class in a dataframe. The file I'm using is for classifying letters so it looks something like this:

    class     feature 1    feature 2    feature 3   feature 4 
0   A         2            3            4           1
1   A         3            1            1           0
2   B         6            7            8           8

Is there a simple way I can only find the mean for "A" for each feature?

answered question

df[df['class'] == 'A'].mean()?

2 Answers

7

You could use this to get the mean for the rows in the class column with value A

df[df['class']=='A'].groupby('class')[list(df)[1:]].mean()

Output

         feature 1  feature 2  feature 3  feature 4
feature                                                   
A              2.5        2.0        2.5        0.5

This will give the mean for the 4 feature n columns.

posted this
11

Surprisingly, groupby is 15% faster than the selection-based approach proposed in the comments (df[df['class'] == 'A'].mean()):

df.groupby('class').mean().loc['A']
#feature 1    2.5
#feature 2    2.0
#feature 3    2.5
#feature 4    0.5

posted this

Have an answer?

JD

Please login first before posting an answer.