欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

搞定Pandas数据排序:sort_index和sort_values的妙用!——从索引排序到按值排序

最编程 2024-01-15 13:24:02
...

sort_index(axis=0, level=None, ascending=True, inplace=False, kind=‘quicksort’, na_position=‘last’,sort_remaining=True)

上述方法中常用参数:

axis:轴索引(排序的方向),0表示按index,1表示按columns

level:若不为None,则对指定索引级别的值进行排序

ascending:是否升序排列,默认为True,表示升序

inplace:默认为False,表示对数据表进行排序,不创建新的实例

kind:选择排序算法

这里只举例kind参数的作用,其他参数在下文的应用中会被用到
arrays = [np.array(['qux', 'qux', 'foo', 'foo',
                    'baz', 'baz', 'bar', 'bar']),
          np.array(['two', 'one', 'two', 'one',
                    'two', 'one', 'two', 'one'])]
s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=arrays)
print("s:\n", s)
print("s.sort_index(level=0):\n", s.sort_index(level=0))
print("s.sort_index(level=1:\n", s.sort_index(level=1))

输出结果:

s:
qux  two    1
     one    2
foo  two    3
     one    4
baz  two    5
     one    6
bar  two    7
     one    8
dtype: int64

s.sort_index(level=0):
bar  one    8
     two    7
baz  one    6
     two    5
foo  one    4
     two    3
qux  one    2
     two    1
dtype: int64

s.sort_index(level=1:
bar  one    8
baz  one    6
foo  one    4
qux  one    2
bar  two    7
baz  two    5
foo  two    3
qux  two    1
dtype: int64

对Series排序

import pandas as pd
import numpy as np

ser_obj = pd.Series(range(10, 15), index=[5, 1, 3, 1, 2])
print("ser_obj:\n", ser_obj)
print("sort:\n", ser_obj.sort_index())  # 升序排列
print("Descending order:\n", ser_obj.sort_index(ascending=False))  # 降序排列

输出结果:

ser_obj:
 5    10
1    11
3    12
1    13
2    14
dtype: int64
sort:
 1    11
1    13
2    14
3    12
5    10
dtype: int64
Descending order:
 5    10
3    12
2    14
1    11
1    13
dtype: int64

对DataFrame排序

df_obj = pd.DataFrame(np.arange(12).reshape(3, 4), index=[2, 1, 3])
print("df_obj:\n", df_obj)
print("sort:\n", df_obj.sort_index())
print("Descending order:\n", df_obj.sort_index(axis=1, ascending=False))
# 按columns进行降序排序

输出结果:

df_obj:
    0  1   2   3
2  0  1   2   3
1  4  5   6   7
3  8  9  10  11
sort:
    0  1   2   3
1  4  5   6   7
2  0  1   2   3
3  8  9  10  11
Descending order:
     3   2  1  0
2   3   2  1  0
1   7   6  5  4
3  11  10  9  8