Reputation: 3810
Suppose you have the following Dataframe (It is much more complicated)
df4=pd.DataFrame(np.matrix([[1,5],[3,2],[4,3],[5,4],[2,1]]),index=['a','b','c','d','e'])
Which is already ranked, however, I would like to rank it by the row index to reach the desired dataframe as
df5=pd.DataFrame(np.matrix([['a','e'],['e','b'],['b','c'],['c','d'],['d','a']]))
Is there an easy way of doing so?
Thank you very much
Upvotes: 0
Views: 248
Reputation: 3810
I would like to rank the matrix based on index. I believe that the posted solution is not right for my question
I had to write a formula to answer this question
def Column_rank_based_list(f):
r,c=f.shape
B= array(range(r*c), dtype='a5').reshape(r,c)
for j in range(c):
for i in range(r):
B[f.ix[i,j]-1,j]=f.index[i]
return pd.DataFrame(B, columns=f.columns)
However, I am having difficulty because it is printing b before the entries.
For example for
df4=pd.DataFrame(np.matrix([[1,5],[3,2],[4,3],[5,4],[2,1]]),index=['a','b','c','d','e'])
you would obtain
Column_rank_based_list(df4)
Upvotes: 0
Reputation: 33803
Pass df4
as an indexer to the index of df4
:
pd.DataFrame(df4.index[df4-1])
Note that I subtracted 1 from df4
since Pandas indexing is zero based, but your DataFrame appears to be 1 based.
The resulting output:
0 1
0 a e
1 c b
2 d c
3 e d
4 b a
Upvotes: 4