我想找到每个位置的整体得分最高的球员。在大熊猫中做这件事的最好,最紧凑的方法是什么?
Name Overall Potential Club Position
L. Messi 94 94 FC Barcelona RF
Ronaldo 94 94 Juventus ST
Neymar Jr 92 93 Paris Saint-Germain LW
De Gea 91 93 Manchester United GK
K. De Bruyne 91 92 Manchester City RCM
E. Hazard 91 91 Chelsea LF
L. Modrić 91 91 Real Madrid RCM
L. Suárez 91 91 FC Barcelona RS
Sergio Ramos 91 91 Real Madrid RCB
J. Oblak 90 93 Atlético Madrid GK
R. Lewandowski 90 90 FC Bayern München ST
T. Kroos 90 90 Real Madrid LCM
我尝试过:
fifa.groupby(by = ["Position"])['Overall'].max()
跟着
fifa.loc[(fifa["Position"] == "CAM") & (fifa['Overall'] == 89),:]
但是,由于职位中的类别太多,这是一项繁琐的任务。
答案 0 :(得分:0)
您可以尝试以下方法:
df[df["Overall"]==df["Overall"].max()]
这会有所帮助。
答案 1 :(得分:0)
使用DataFrame.drop_duplicates
(假设Overall
列已排序):
df = df.drop_duplicates(subset=['Position'], keep='first')
print(df)
Name Overall Potential Club Position
0 L. Messi 94 94 FC Barcelona RF
1 Ronaldo 94 94 Juventus ST
2 Neymar Jr 92 93 Paris Saint-Germain LW
3 De Gea 91 93 Manchester United GK
4 K. De Bruyne 91 92 Manchester City RCM
5 E. Hazard 91 91 Chelsea LF
7 L. Suárez 91 91 FC Barcelona RS
8 Sergio Ramos 91 91 Real Madrid RCB
11 T. Kroos 90 90 Real Madrid LCM
答案 2 :(得分:0)
您可以将中间结果与原始数据框合并以获得完整行:
pd.DataFrame(df.groupby('Position')['Overall'].max()).reset_index().merge(df,
on=['Position', 'Overall'])
它给出:
Position Overall Name Potential Club
0 GK 91 De Gea 93 Manchester United
1 LCM 90 T. Kroos 90 Real Madrid
2 LF 91 E. Hazard 91 Chelsea
3 LW 92 Neymar Jr 93 Paris Saint-Germain
4 RCB 91 Sergio Ramos 91 Real Madrid
5 RCM 91 K. De Bruyne 92 Manchester City
6 RCM 91 L. Modrić 91 Real Madrid
7 RF 94 L. Messi 94 FC Barcelona
8 RS 91 L. Suárez 91 FC Barcelona
9 ST 94 Ronaldo 94 Juventus
您可以注意到RCM位置的前2个。