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

后端 未结 8 1588
被撕碎了的回忆
被撕碎了的回忆 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:20

    To sort a column descending, while maintaining the index ascending:

    import pandas as pd
    df = pd.DataFrame(index=range(5), data={'c': [4,2,2,4,2]})
    df.index = df.index[::-1]
    print df.sort(column='c', ascending=False)
    

    Output:

       c
    1  4
    4  4
    0  2
    2  2
    3  2
    

提交回复
热议问题