Python 如何使用熊猫找到大学排名和全球排名?

Python 如何使用熊猫找到大学排名和全球排名?,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我有以下数据框: Agent_Name college_name score college_local_ranking global_ranking Anna Harvard 60 1 4 Mathew oxford 99 1 1 Angel IIT 65 3

我有以下数据框:

Agent_Name college_name  score  college_local_ranking  global_ranking
Anna          Harvard      60               1             4
Mathew        oxford       99               1             1
Angel         IIT          65               3             6
我能用排名函数找到全局排名

df['global_ranking'] = df['score'].rank(ascending=False)
请帮我根据他们学校的分数找到当地的排名。 我试过了,但有点错误

df['college_local_ranking'] = df['score'].groupby(by = ['college_name']).rank()
你的命令:
df['college\u local\u ranking']=df['score'].groupby(by=['college\u name']).rank()

将失败,因为您正在使用
df[score]
对数据框进行子集设置,然后在
college\u name
上应用groupby,该子集中不存在

正确的命令应该是:

df['college_local_ranking'] = df.groupby('college_name')['score'].rank()
你的命令:
df['college\u local\u ranking']=df['score'].groupby(by=['college\u name']).rank()

将失败,因为您正在使用
df[score]
对数据框进行子集设置,然后在
college\u name
上应用groupby,该子集中不存在

正确的命令应该是:

df['college_local_ranking'] = df.groupby('college_name')['score'].rank()

这是对你有用的

df.college_local_ranking=df.groupby("college_name")["score"].rank(ascending=False)

希望对你有所帮助这里有一些对你有用的东西

df.college_local_ranking=df.groupby("college_name")["score"].rank(ascending=False)

希望这会有所帮助

这与上面Mayank的答案非常相似。当我开始回答时,他的回答不在那里,所以再次为同样的回答道歉。这与上面Mayank的回答非常相似。当我开始回答时,他的回答不在那里,所以再次为同样的回答道歉。