Sort pandas dataframe both on values of a column and index?

后端 未结 8 1635
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 08:43

Is it feasible to sort pandas dataframe by values of a column, but also by index?

If you sort a pandas dataframe by values of a column, you can get the resultant dat

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 09:19

    In pandas 0.23+ you can do it directly - see OmerB's answer. If you don't yet have 0.23+, read on.


    I'd venture that the simplest way is to just copy your index over to a column, and then sort by both.

    df['colFromIndex'] = df.index
    df = df.sort(['count', 'colFromIndex'])
    

    I'd also prefer to be able to just do something like df.sort(['count', 'index']), but of course that doesn't work.

提交回复
热议问题