Home > Software design >  sorting single row dataframe by column values
sorting single row dataframe by column values

Time:11-12

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.

CodePudding user response:

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

CodePudding user response:

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!

  • Related