如何找出哪个候选人[男性或女性]在每个城市得分最高?

How to find out which canidate [Male or Female] scored maximum points in each city?

我有这个数据框,我想找出每个城市谁获得了最高分。

import pandas as pd 

df = pd.DataFrame({"City":["Delhi","Delhi","Mumbai","Mumbai","Lahore","Lahore"],
"Points":[90.1,90.3,94.1,95,89,90.5],
"Gender":["Male","Female","Female","Male","Female","Male"]})

到目前为止我已经试过了,但这给了我每个城市的性别 [男性和女性] 的排行榜,

df.groupby(by=["City","Gender"],sort=False)["Points"].max()

我想要一个在每个城市得分最高的候选人[男性或女性]。

尝试使用仅 'City' 作为索引的枢轴 table:

df.pivot_table(values='Points',index=['City'],aggfunc='max')

#输出

        Points
City          
Delhi     90.3
Lahore    90.5
Mumbai    95.0

如果你想使用 groupby,请随意使用,但再次提醒,请记住只使用 'City',性别无关紧要

df.groupby(by=["City"],sort=False)["Points"].max()
#Output
City
Delhi     90.3
Mumbai    95.0
Lahore    90.5
Name: Points, dtype: float64

如果你还想看到性别,使用这个:

df.groupby('City').apply(lambda x: x.sort_values('Points').iloc[-1].reset_index(drop=True))

输出

    0   1   2
City            
Delhi   Delhi   90.3    Female
Lahore  Lahore  90.5    Male
Mumbai  Mumbai  95.0    Male

这个只会给你 table 个最高分

import pandas as pd 

df = pd.DataFrame({
    "City":["Delhi","Delhi","Mumbai","Mumbai","Lahore","Lahore"],
    "Points":[90.1,90.3,94.1,95,89,90.5],
    "Gender":["Male","Female","Female","Male","Female","Male"]
})

df.groupby('City').apply(lambda x: x.sort_values('Points').tail(1)).reset_index(drop=True)

输出:

     City  Points  Gender
0   Delhi    90.3  Female
1  Lahore    90.5    Male
2  Mumbai    95.0    Male