I have a dataframe like this:
color power
red 6
red 8
green 3
red 1
yellow 10
green 5
What I want is this:
color mean_of_power
yellow 10
red 5
green 4
I have tried df.groupby("color")["power"].mean()
, but this will give me a dataframe sorted alphabetically:
color mean_of_power
green 4
red 5
yellow 10
How can I group a dataframe by one column (color
), calculate the mean of another column (power
) per group and sort the output by the value of that mean?
CodePudding user response:
You can add sort_values
right after that:
df.groupby("color").power.mean().sort_values(ascending=False)
CodePudding user response:
df.groupby("color")["power"].mean().sort_values('mean_of_power')