user_id_2 1 2 3 4 5
user_id_1
1 0.000000 0.707107 0.388075 0.668153 0.559017
2 0.707107 0.000000 0.504916 0.491354 0.632456
>> 3 0.388075 0.504916 0.000000 0.373383 0.225618 <<
4 0.668153 0.491354 0.373383 0.000000 0.448211
5 0.559017 0.632456 0.225618 0.448211 0.000000
These are the results of some computations. I was wondering if I could choose a row and rank the highest value with the columns.
Eg. Choosing row user_id_1(3)
user_id_1 user_id_2
3 2
3 1
3 4
3 5
3 3
Use Series.argsort
in descending order of selected row by DataFrame.loc
, get new order of columns names and create new DataFrame by constructor:
val = 3
df = pd.DataFrame({'user_id_1':val,
'user_id_2':df.columns[(-df.loc[val]).argsort()]})
print (df)
user_id_1 user_id_2
0 3 2
1 3 1
2 3 4
3 3 5
4 3 3
Or if want only order add 1
:
val = 3
df = pd.DataFrame({'user_id_1':val,
'user_id_2':(-df.loc[val]).argsort() + 1})
print (df)
user_id_1 user_id_2
1 3 2
2 3 1
3 3 4
4 3 5
5 3 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.