如何使用列名而不是值创建排名矩阵?

时间:2018-03-03 01:34:30

标签: python pandas dataframe

我对Python很陌生。 我有一个像以下

的数据框
       book 1      book 2     book 3  
user 1  0.05       0.47       0.09
user 2  0.3        0.01       0.35

我希望它根据值(降序)排序,输出如下:

        Rank 1      Rank 2      Rank 3 
user1   book 2      book 3      book 1
user2   book 3      book 1      book 2  

this is how the matrix looks in real task

感谢您的帮助。

谢谢。

4 个答案:

答案 0 :(得分:1)

这是一种方法。

pandas.DataFrame([df.columns[x] for x in np.argsort(-df.values)], 
                 index=df.index, 
                 columns=['rank' + str(i + 1) for i in range(df.shape[1])])

       rank1  rank2
user1  book2  book1
user2  book1  book2

答案 1 :(得分:0)

new_cols = {'book %i' % i: 'Rank %i' % i for i in range(1, df.shape[1]+1)}
df.apply(lambda s: s.index[s.argsort()][::-1], axis=1).rename(new_cols, axis=1)

返回

        Rank 1  Rank 2  Rank 3
user 1  book 2  book 3  book 1
user 2  book 3  book 1  book 2

答案 2 :(得分:0)

您可以使用apply:

(
     df.apply(lambda x: x.sort_values().index[::-1], axis=1)
     .rename(columns=lambda x: x.replace('book','Rank'))
)
Out[9]: 
        Rank 1  Rank 2
user 1  book 2  book 1
user 2  book 1  book 2

答案 3 :(得分:0)

使用numpy.argsort

注意:正如@ely在评论中提到的那样,axis=1不是必需的

df = pd.DataFrame((-df.values).argsort() + 1, index=df.index)
#same as
#df = pd.DataFrame((-df.values).argsort(axis=1) + 1, index=df.index)
print (df)
        0  1  2
user 1  2  3  1
user 2  3  1  2

更改DataFrame中的列名称和值:

df.columns = ['Rank {}'.format(x+1) for x in df.columns]
df = 'book ' + df.astype(str)
print (df)
        Rank 1  Rank 2  Rank 3
user 1  book 2  book 3  book 1
user 2  book 3  book 1  book 2
相关问题