我有 1000 行的資料。示例結構:
在此處輸入影像描述
我需要找到:
determine the winners (surnames and names of children) in each age group for boys
and for girls (3 first places) for each species. Keep in mind that the winners can be
more than 3, as the results may coincide
我嘗試按日期和跑步進行雙重分組,但我需要在一年內有3個位置,并且每個跑步時間位置有無限多的運動員
我的代碼的一個例子:
df_agg_girl = data[data['sex']=='ж'].groupby([data.year,data.run1000,data.fullName]).agg("mean")
g = df_agg_girl['run1000'].groupby(['year','run1000'], group_keys=False)
res = g.apply(lambda x: x.sort_values(ascending=False).nsmallest(3,keep='all'))
print(res)
我真的不明白該怎么做
uj5u.com熱心網友回復:
您能否提供有關列的更多資訊?請嘗試分享真實格式。有了我看到的資料,我可以使用它。
df["ranking"] = df.groupby(["year",'sex'])["run"].rank(method="dense", ascending=True)
print(df[df.ranking <= 3])
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/532339.html
下一篇:熊貓列中按升序排序的問題
