Pandas数据排序——【按索引排序sort_index()方法、按值排序sort_value()方法】


按索引排序——sort_index()

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

按值排序——sort_value()

sort_value(by, axis=0, ascending=True, inplace=False, kind=‘quicksort’, na_position=‘last’)

上述方法中常用参数:

by: 表示排序的列

na_position:为first则将NaN值放在开头;为last则将NaN值放在最后

对Series进行排序

ser_obj1 = pd.Series([4, np.nan, 6, np.nan, -3, 2])
print("ser_obj1:\n", ser_obj1)
print("ser_obj1.sort_values():\n", ser_obj1.sort_values())  # 按值升序排列
print("sort_values(na_position='first'):\n", ser_obj1.sort_values(na_position='first'))

输出结果:

ser_obj1:
 0    4.0
1    NaN
2    6.0
3    NaN
4   -3.0
5    2.0
dtype: float64
ser_obj1.sort_values():
 4   -3.0
5    2.0
0    4.0
2    6.0
1    NaN
3    NaN
dtype: float64
sort_values(na_position='first'):
 1    NaN
3    NaN
4   -3.0
5    2.0
0    4.0
2    6.0
dtype: float64


对DataFrame进行排序

df_obj1 = pd.DataFrame([[0.4, -0.1, -0.3, 0.0],
                        [0.2, 0.6, -0.1, -0.7],
                        [0.8, 0.6, -0.5, 0.1]])
print("df_obj1:\n", df_obj1)
print("df1.value:\n", df_obj1.sort_values(by=2, ascending=False))
# 对列索引为2的数据进行降序排序

输出结果:

df_obj1:
      0    1    2    3
0  0.4 -0.1 -0.3  0.0
1  0.2  0.6 -0.1 -0.7
2  0.8  0.6 -0.5  0.1
df1.value:
      0    1    2    3
1  0.2  0.6 -0.1 -0.7
0  0.4 -0.1 -0.3  0.0
2  0.8  0.6 -0.5  0.1

猜你喜欢

转载自blog.csdn.net/Jormungand_V/article/details/109703497
今日推荐