sorting single row dataframe by column values

Question:

I have to sort columns in single row dataframe by descending order.

dataframe looks like:

   store_1  store_2  store_3
0       11       54       28

result should be like:

   store_2  store_3  store_1
0       54       28       11

dataframe has more than sixty columns.

Asked By: Wild_rasta

||

Answers:

This should work:

df.sort_values(by=0,axis=1) 

where by indicates the label or index of the row, and axis = 1 indicates you want to sort the columns!

Answered By: AndreaYolo

You can use the pandas.Series.sort_values:

import pandas as pd

df = pd.DataFrame(data=[[11,54,28]], columns=['store_1', 'store_2', 'store_3'])
df = df.sort_values(by=0, axis=1, ascending=False)
print(df)

output will be:

   store_2  store_3  store_1
0       54       28       11
Answered By: Giuseppe La Gualano
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.