1 回答

TA贡献1852条经验 获得超1个赞
您可以按 wickets 降序排序,创建一个将 Role 映射到数据帧切片的字典,然后根据您的规则连接结果。
# sort by 'wickets_taken'
df = df.sort_values('wickets_taken', ascending=False)
# group by 'Role'
g = dict(tuple(df.groupby('Role')))
# concatenate custom slices
res = pd.concat([g['FastBowler'].iloc[:2],
g['SpinBowler'],
g['FastBowler'].iloc[2:],
g['Batsman+FastBowler']],
ignore_index=True)
结果
print(res)
Name Role run_scored wickets_taken
0 P FastBowler 62 13
1 O FastBowler 32 9
2 Q SpinBowler 65 12
3 T SpinBowler 17 6
4 K FastBowler 24 7
5 N Batsman+FastBowler 114 8
添加回答
举报