如何使用Python Pandas对组的平均值进行排序



数据帧有国家、高度和许多其他列。我想按国家报告平均身高,并按最高平均身高排序。我陷入了困境。到目前为止,我有这个。

cH = df.("Country")["Height"].mean()

这让我可以找到每个国家的平均身高。然而,现在我需要对此进行分类。

试试这个:

df= pd.DataFrame({
'Country':['a','b','a','b'],
'Height':[10,20,30,40]
})
df.groupby("Country", as_index=False).Height.mean().sort_values('Height', ascending=False)

输出

Country Height
1   b       30
0   a       20

最新更新