import pandas as pd
df = pd.DataFrame({'姓名':['甲','乙','丙','丁','戊','己','庚'], '成绩':range(70,0,-10), '性别':['男','男','男','女','女','女','女'], 'd':[0,1,2,0,1,2,3] })
#假设原数据没有'd'列
df2 = df[['姓名','成绩','性别']]
#直接用性别分组可以得到分组统计
grouped = df2.groupby('性别')
print(grouped.mean())
#使用 rank 函数得到性别分组排号
df2['d'] = df2['成绩'].groupby(df2['性别']).rank(ascending = 0)
print(df2)