简体   繁体   English

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

[英]How to create a rank matrix with column names instead of values?

I am quite new to Python.我对 Python 很陌生。 I have a data frame like the following我有一个如下所示的数据框

       book 1      book 2     book 3  
user 1  0.05       0.47       0.09
user 2  0.3        0.01       0.35

I want it to be ranked based on the values(descending order) and the output to be the following:我希望它根据值(降序)进行排名,输出如下:

        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这就是矩阵在实际任务中的样子

I would appreciate your help.我会很感激你的帮助。

Thank you.谢谢你。

Here's one approach.这是一种方法。

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
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)

returns返回

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

You can use 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

Use numpy.argsort :使用numpy.argsort

Notice: as @ely mentioned in comment, axis=1 is not necessary注意:正如@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

Change columns names and values in DataFrame :更改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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM