输入数据框:
load1 = pd.DataFrame({'A':list('abcdef'),
'B':[4,5,4,5,5,4],
})
Rank
:按降序对B值进行排序,并按递增顺序从1开始给予排名
Rank_without_a_column
:保留降序排列的B的第一列,并以递增顺序给出从1开始的排名
Exact_Rank
:给出正确的排名,如“预期输出”上的“精确排名”所示
Exact_Rank_Without_a_column
:
预期输出:
A B Rank Rank_without_a_column Exact_Rank Exact_Rank_Without_a_column
0 a 5 1 Null 1 Null
1 b 5 2 1 1 1
2 c 5 3 2 1 1
3 d 4 4 3 2 2
4 e 4 5 4 2 2
5 f 4 6 5 2 2
答案 0 :(得分:1)
您需要一系列的等级方法,例如:
load1.sort_values('B',ascending=False,inplace=True)
load1['Rank'] = load1['B'].rank(ascending=False,method='first').astype(int)
load1.reset_index(drop=True,inplace=True)
load1.loc[1:,'Rank_without_a_column'] = load1.loc[1:,'B'].rank(ascending=False,method='first')
load1['Exact_Rank'] = load1['B'].rank(ascending=False,method='dense').astype(int)
load1.loc[1:,'Exact_Rank_Without_a_column'] = load1.loc[1:,'Exact_Rank'].rank(ascending=True,method='dense')
load1
A B Rank Rank_without_a_column Exact_Rank Exact_Rank_Without_a_column
0 b 5 1 NaN 1 NaN
1 d 5 2 1.0 1 1.0
2 e 5 3 2.0 1 1.0
3 a 4 4 3.0 2 2.0
4 c 4 5 4.0 2 2.0
5 f 4 6 5.0 2 2.0