Convert the headings of a matrix into a list and sort them according to one row
AnsweredHello,
I want to gernerate a list of products which is sorted by the mean of the products.
I have a pandas matrix like this
Product1 Product2 Product3
freq. 253 158 459
mean 29,03 15,54 37,57
std. 25,00 18,28 23,01
and the result should be:
All_Products= ['Product2','Product1','Product3']
Is there a possibility to create such a list? I am thankful for any help!
Best regards!
Marc
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi Marc,
This is not really a Gurobi question. You should be able to get this done using sort_values. You can specify the axis to sort column-wise instead of along the index and you can slice a DataFrame using loc[mean].
df = pd.DataFrame()
df['Product1'] = [253, 29.03, 25]
df['Product2'] = [158, 15.54, 18.28]
df['Product3'] = [459, 37.57, 23.01]
df.index = ['freq','mean','std']
df.sort_values(by='mean', axis=1).columnsreturns:
Index(['Product2', 'Product1', 'Product3'], dtype='object')
Cheers,
Matthias0 -
Thank you Matthias! The links are very helpful.
Have a nice weekend !
0
Post is closed for comments.
Comments
3 comments