[英]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'.但正如您所看到的,它仅适用于“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.您的代码有效 - 但排序的工作方式是它是分层的,如果 A 列中的每个值都不同,那么它将首先强制在 A 列上进行排序。然后它会在 B 列上对存在多个的所有实例进行排序A 中的相同值,并且仅当存在 A 和 B 相同但 C 不同的情况时才适用于 C。
However - all your values shown in A are different from one another so it will only sort based on A using this method.但是 - 您在 A 中显示的所有值都彼此不同,因此它只会使用此方法基于 A 进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.