简体   繁体   中英

How to sort all columns independently in ascending order in a pandas dataframe?

I would like to sort in ascending order all the columns in a dataframe independently. My data frame is as follows:

date,A,B,C,D
1989-12-31,540.8,497.351,757.9,649.811
1990-12-31,388.9,453.65,454.2,714.898
1991-12-31,796.3,170.308,1080.4,274.678
1992-12-31,427.7,304.587,695.6,414.898

I have tried manually:

df1=df.sort_values(by=['A','B','C','D'],axis=0, inplace=True)

date,A,B,C,D
1990-12-31,388.9,453.65,454.2,714.898
1992-12-31,427.7,304.587,695.6,414.898
1989-12-31,540.8,497.351,757.9,649.811
1991-12-31,796.3,170.308,1080.4,274.678

But as you can see it works only with column 'A'.

Do I have to do a loop on each column?

Is there a simpler way? I have had a look in the sort manual but I am not able to figure it out.

Thanks

Your code worked - but the way sorting works is that it is hierarchical, if every value in column A is different, then it will force the sort first on column A. It will then sort on column B for all instances where there are multiple of the same value in A, and for C only if there are instances where both A and B are the same, but C differs.

However - all your values shown in A are different from one another so it will only sort based on A using this method.

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.

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