简体   繁体   English

如何在熊猫数据框中按升序对所有列进行独立排序?

[英]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.

相关问题 使用sort_values()独立地对pandas DataFrame的所有列进行排序 - Sort all columns of a pandas DataFrame independently using sort_values() pandas重新排列数据帧,使每列的所有值都按升序排列 - pandas rearrange dataframe to have all values in ascending order per every column independently 如何使用 Python 按升序对 pandas dataframe 进行排序 - How to sort pandas dataframe in ascending order using Python 如何动态独立地移动pandas DataFrame中的列? - how to shift columns in pandas DataFrame dynamically and independently? 将唯一 ID 分配给 Pandas 数据框中两列的组合,按其顺序独立 - Assign unique ID to combination of two columns in pandas dataframe independently on their order Pandas 对多列独立排序 - Pandas sort multiple columns independently 对 pandas DataFrame 列先按年份降序排序,然后按月份升序排序 - Sort pandas DataFrame columns first based on year descending and then months ascending 如何按特定顺序在两个(或更多)不同的列上对 pandas dataframe 进行排序 - How to sort a pandas dataframe on two (or more) different columns, in a particular order 按升序对pandas DataMatrix进行排序 - Sort a pandas DataMatrix in ascending order 如何在熊猫中的每个组中按日期时间升序应用排序 - How to apply sort by ascending order for datetime in every group in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM