In Pandas How to sort one level of a multi-index based on the values of a column, while maintaining the grouping of the other level

后端 未结 1 1226
野的像风
野的像风 2021-02-09 00:53

I\'m taking a Data Mining course at university right now, but I\'m a wee bit stuck on a multi-index sorting problem.

The actual data involves about 1 million reviews of

1条回答
  •  被撕碎了的回忆
    2021-02-09 01:24

    You're looking for sort:

    In [11]: s = pd.Series([3, 1, 2], [[1, 1, 2], [1, 3, 1]])
    
    In [12]: s.sort()
    
    In [13]: s
    Out[13]: 
    1  3    1
    2  1    2
    1  1    3
    dtype: int64
    

    Note; this works inplace (i.e. modifies s), to return a copy use order:

    In [14]: s.order()
    Out[14]: 
    1  3    1
    2  1    2
    1  1    3
    dtype: int64
    

    Update: I realised what you were actually asking, and I think this ought to be an option in sortlevels, but for now I think you have to reset_index, groupby and apply:

    In [21]: s.reset_index(name='s').groupby('level_0').apply(lambda s: s.sort('s')).set_index(['level_0', 'level_1'])['s']
    Out[21]: 
    level_0  level_1
    1        3          1
             1          3
    2        1          2
    Name: 0, dtype: int64
    

    Note: you can set the level names to [None, None] afterwards.

    0 讨论(0)
提交回复
热议问题