我有这个df,我需要对其进行汇总然后进行排序:
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 player_slug 692 non-null object
1 player_id 692 non-null int64
2 player_nickname 692 non-null object
3 player_team 692 non-null int64
4 player_position 692 non-null object
5 price_cartoletas 692 non-null float64
6 score_mean 692 non-null float64
7 score_no_cleansheets_mean 692 non-null float64
8 diff_home_away_s 692 non-null float64
9 n_games 692 non-null int64
10 score_mean_home 692 non-null float64
11 score_mean_away 692 non-null float64
12 shots_x_mean 692 non-null float64
13 fouls_mean 692 non-null float64
14 RB_mean 692 non-null float64
15 PE_mean 692 non-null float64
16 A_mean 692 non-null float64
17 I_mean 692 non-null float64
18 FS_mean 692 non-null float64
19 FF_mean 692 non-null float64
20 G_mean 692 non-null float64
21 DD_mean 692 non-null float64
22 DP_mean 692 non-null float64
23 status 692 non-null object
24 price_diff 692 non-null float64
25 last_points 692 non-null float64
我首先这样做:
df_agg = medias.groupby('player_slug').agg({'price_cartoletas':['mean']})
它给了我
DF_TEMP price_cartoletas
mean
player_slug
abner-felipe 5.09
abner-vinicius 5.28
adalberto 1.72
adilson 8.75
adriano 9.44
... ...
yuri 1.99
yuri-lima 2.04
ze-marcos 1.64
ze-rafael 10.35
zeca 7.31
但是当我尝试对其进行排序时:
df_agg.sort_values('price_cartoletas', ascending=False)
我得到:
ValueError: The column label 'price_cartoletas' is not unique.
For a multi-index, the label must be a tuple with elements corresponding to each level.
我该如何排序?
答案 0 :(得分:4)
您应该删除[]
,这将在列上创建多个索引
df_agg = medias.groupby('player_slug').agg({'price_cartoletas':'mean'}).sort_values('price_cartoletas', ascending=False)
如果您想要多个索引,我们可以从sort_values
df_agg = df_agg.sort_values(('price_cartoletas','mean'), ascending=False)