I have a dataframe with 3 columns
Alpha Bravo Charlie
20 30 40
50 10 20
40 60 10
I wish to create 3 new columns with rankings that produces the following that gives the highest among the 3 columns a rank of 3 to 1:
AlphaRank BravoRank CharlieRank
1 2 3
3 1 2
2 3 1
I understand there is dataframe.rank function but I only saw example for 1 column not 3
I tried this with issues:
for newrank in ['Alpha', 'Bravo', 'Charlie']:
ranksys = df[newrank]
ranksystem = newrank +'Rank'
df[ranksystem] = ranksys.rank(axis=1).astype(int)
cols = ['Alpha', 'Bravo', 'Charlie']
df[cols] = df[cols].rank().astype(int)
print (df)
Alpha Bravo Charlie
0 1 2 3
1 3 1 2
2 2 3 1
Numpy alternative with numpy.argsort
:
df[cols] = pd.DataFrame(df[cols].values.argsort(axis=0) + 1,index=df.index,columns=df.columns)