[英]Sort all columns of a pandas DataFrame independently using sort_values()
I have a dataframe and want to sort all columns independently in descending or ascending order. 我有一个数据框,并希望以降序或升序独立排序所有列。
import pandas as pd
data = {'a': [5, 2, 3, 6],
'b': [7, 9, 1, 4],
'c': [1, 5, 4, 2]}
df = pd.DataFrame.from_dict(data)
a b c
0 5 7 1
1 2 9 5
2 3 1 4
3 6 4 2
When I use sort_values() for this it does not work as expected (to me) and only sorts one column: 当我为此使用sort_values()时 ,它不能按预期工作(对我来说)并且只对一列进行排序:
foo = df.sort_values(by=['a', 'b', 'c'], ascending=[False, False, False])
a b c
3 6 4 2
0 5 7 1
2 3 1 4
1 2 9 5
I can get the desired result if I use the solution from this answer which applies a lambda function: 如果我使用这个应用lambda函数的答案的解决方案,我可以得到所需的结果:
bar = df.apply(lambda x: x.sort_values().values)
print(bar)
a b c
0 2 1 1
1 3 4 2
2 5 7 4
3 6 9 5
But this looks a bit heavy-handed to me. 但这对我来说看起来有点笨拙。
What's actually happening in the sort_values() example above and how can I sort all columns in my dataframe in a pandas-way without the lambda function? 上面的sort_values()示例中实际发生了什么,如何在没有lambda函数的情况下以pandas方式对数据框中的所有列进行排序?
You can use numpy.sort
with DataFrame
constructor: 您可以将
numpy.sort
与DataFrame
构造函数一起使用:
df1 = pd.DataFrame(np.sort(df.values, axis=0), index=df.index, columns=df.columns)
print (df1)
a b c
0 2 1 1
1 3 4 2
2 5 7 4
3 6 9 5
EDIT: 编辑:
Answer with descending order: 按降序回答:
arr = df.values
arr.sort(axis=0)
arr = arr[::-1]
print (arr)
[[6 9 5]
[5 7 4]
[3 4 2]
[2 1 1]]
df1 = pd.DataFrame(arr, index=df.index, columns=df.columns)
print (df1)
a b c
0 6 9 5
1 5 7 4
2 3 4 2
3 2 1 1
sort_values
will sort the entire data frame by the columns order you pass to it. sort_values
将按照传递给它的列顺序对整个数据框进行排序。 In your first example you are sorting the entire data frame with ['a', 'b', 'c']
. 在您的第一个示例中,您使用
['a', 'b', 'c']
对整个数据框进行排序。 This will sort first by 'a'
, then by 'b'
and finally by 'c'
. 这首先按
'a'
排序,然后按'b'
排序,最后按'c'
排序。
Notice how, after sorting by a
, the rows maintain the same. 请注意,在按
a
排序后,行保持相同。 This is the expected result. 这是预期的结果。
Using lambda
you are passing each column to it, this means sort_values
will apply to a single column, and that's why this second approach sorts the columns as you would expect. 使用
lambda
将每列传sort_values
它,这意味着sort_values
将应用于单个列,这就是为什么第二种方法按预期对列进行排序的原因。 In this case, the rows change. 在这种情况下,行会改变。
If you don't want to use lambda
nor numpy
you can get around using this: 如果你不想使用
lambda
或numpy
你可以使用它:
pd.DataFrame({x: df[x].sort_values().values for x in df.columns.values})
Output: 输出:
a b c
0 2 1 1
1 3 4 2
2 5 7 4
3 6 9 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.