Search code examples
pythonpandasnumpydataframeaverage

pandas dataframe: finding average of weight for average age samples, group them by gender


So far I can get the average age of the data:

np.mean(df.age)

As well as the average weight sorted by gender:

df.groupby(by='gender')['weight'].mean()

But I don't know how to put the condition of how I need to find the average weight of people who are above the average age only, and show it by gender.


Solution

  • You can filter and groupby:

    mean_age = df['age'].mean()
    
    out = df[df['age']>mean_age].groupby('gender')['weight'].mean()
    

    On the other note, you may want to filter by average age per gender:

    mean_age = df.groupby('gender')['age'].transform('mean')
    
    out = df[df['age']>mean_age].groupby('gender')['weight'].mean()
    

    Once you have that, you can plot with

    out.plot.bar()