python指定列升序排列_在Pandas中按升序和降序对单个列进行条件排序

使用^{},您可以定义一个排序序列。来自@smj的数据。在

设置import pandas as pd

import numpy as np

number_list = list(range(1, 11))

df = pd.DataFrame({'town': sorted(['Springfield', 'Shelbyville'] * 10),

'street': sorted(['Evergreen Terrace', 'Main Street'] * 10),

'number': number_list + number_list})

解决方案

点餐要小心。np.lexsort从序列的最后一个元素开始工作;例如,s1排序优先级最高,s4最低。在

^{pr2}$

结果print(res)

town street number

0 Shelbyville Evergreen Terrace 1

2 Shelbyville Evergreen Terrace 3

4 Shelbyville Evergreen Terrace 5

6 Shelbyville Evergreen Terrace 7

8 Shelbyville Evergreen Terrace 9

9 Shelbyville Evergreen Terrace 10

7 Shelbyville Evergreen Terrace 8

5 Shelbyville Evergreen Terrace 6

3 Shelbyville Evergreen Terrace 4

1 Shelbyville Evergreen Terrace 2

10 Springfield Main Street 1

12 Springfield Main Street 3

14 Springfield Main Street 5

16 Springfield Main Street 7

18 Springfield Main Street 9

19 Springfield Main Street 10

17 Springfield Main Street 8

15 Springfield Main Street 6

13 Springfield Main Street 4

11 Springfield Main Street 2

你可能感兴趣的:(python指定列升序排列)