Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按多列和重复索引对数据帧进行排序_Python_Pandas - Fatal编程技术网

Python 按多列和重复索引对数据帧进行排序

Python 按多列和重复索引对数据帧进行排序,python,pandas,Python,Pandas,我有一个带有重复索引的熊猫数据帧。每个索引有3行,它们对应于一组项目。有两列,a和b df = pandas.DataFrame([{'i': b % 4, 'a': abs(b - 6) , 'b': b} for b in range(12)]).set_index('i') 我想对数据帧进行排序,以便: 具有相同索引的所有行都是相邻的。(所有组都在一起) 各组按组内最低值a的相反顺序排列 例如,在上述df中,前三个项目应该是索引0的项目,因为

我有一个带有重复索引的熊猫数据帧。每个索引有3行,它们对应于一组项目。有两列,
a
b

df = pandas.DataFrame([{'i': b % 4, 'a': abs(b - 6) , 'b': b}
                       for b in range(12)]).set_index('i')
我想对数据帧进行排序,以便:

  • 具有相同索引的所有行都是相邻的。(所有组都在一起)
  • 各组按组内最低值
    a
    的相反顺序排列
  • 例如,在上述
    df
    中,前三个项目应该是索引
    0
    的项目,因为这三行的最低
    a
    值为2,所有其他组至少有一行
    a
    值低于2。第二个三个项目可以是第3组或第1组,因为这两个组中的最低
    a
    值都是1。最后一组项目应该是组2,因为它有一行
    a
    值为0

  • 在每个组中,项目按
    b
    升序排序
  • 期望输出:

    a b i 0 6 0 0 2 4 0 2 8 3 3 3 3 1 7 3 5 11 1 5 1 1 1 5 1 3 9 2 4 2 2 0 6 2 4 10
    但是它给了我一个keyrerror,如果我把
    I
    作为一列而不是索引,它只会走那么远。

    您可以先按
    a
    降序排序,然后对索引排序:

    >>> df.sort(['a', 'b'], ascending=[False, True]).sort_index()
       a   b
    i       
    0  6   0
    0  2   4
    0  2   8
    1  5   1
    1  3   9
    1  1   5
    2  4   2
    2  4  10
    2  0   6
    3  5  11
    3  3   3
    3  1   7
    

    您可以先按
    a
    降序排序,然后对索引进行排序:

    >>> df.sort(['a', 'b'], ascending=[False, True]).sort_index()
       a   b
    i       
    0  6   0
    0  2   4
    0  2   8
    1  5   1
    1  3   9
    1  1   5
    2  4   2
    2  4  10
    2  0   6
    3  5  11
    3  3   3
    3  1   7
    

    我看到的最直接的方法是将索引移动到一列,并使用组min计算一个新列

    In [43]: df = df.reset_index()
    
    In [45]: df['group_min'] = df.groupby('i')['a'].transform('min')
    
    然后,您可以根据您的条件进行排序:

    In [49]: df.sort_values(['group_min', 'i', 'b'], ascending=[False, False, True])
    Out[49]: 
        i  a   b  group_min
    0   0  6   0          2
    4   0  2   4          2
    8   0  2   8          2
    3   3  3   3          1
    7   3  1   7          1
    11  3  5  11          1
    1   1  5   1          1
    5   1  1   5          1
    9   1  3   9          1
    2   2  4   2          0
    6   2  0   6          0
    10  2  4  10          0
    
    要返回到所需帧,请删除跟踪变量并重置索引

    In [50]: df.sort_values(['group_min', 'i', 'b'], ascending=[False, False, True]).drop('group_min', axis=1).set_index('i')
    Out[50]: 
       a   b
    i       
    0  6   0
    0  2   4
    0  2   8
    3  3   3
    3  1   7
    3  5  11
    1  5   1
    1  1   5
    1  3   9
    2  4   2
    2  0   6
    2  4  10
    

    我看到的最直接的方法是将索引移动到一列,并使用组min计算一个新列

    In [43]: df = df.reset_index()
    
    In [45]: df['group_min'] = df.groupby('i')['a'].transform('min')
    
    然后,您可以根据您的条件进行排序:

    In [49]: df.sort_values(['group_min', 'i', 'b'], ascending=[False, False, True])
    Out[49]: 
        i  a   b  group_min
    0   0  6   0          2
    4   0  2   4          2
    8   0  2   8          2
    3   3  3   3          1
    7   3  1   7          1
    11  3  5  11          1
    1   1  5   1          1
    5   1  1   5          1
    9   1  3   9          1
    2   2  4   2          0
    6   2  0   6          0
    10  2  4  10          0
    
    要返回到所需帧,请删除跟踪变量并重置索引

    In [50]: df.sort_values(['group_min', 'i', 'b'], ascending=[False, False, True]).drop('group_min', axis=1).set_index('i')
    Out[50]: 
       a   b
    i       
    0  6   0
    0  2   4
    0  2   8
    3  3   3
    3  1   7
    3  5  11
    1  5   1
    1  1   5
    1  3   9
    2  4   2
    2  0   6
    2  4  10
    

    谢谢这只是错过了最后一个标准,即项目也按
    b
    分组排序。我没有看到这一点。修改为包含
    b
    。谢谢!这只是错过了最后一个标准,即项目也按
    b
    分组排序。我没有看到这一点。修改为包含
    b