How to sort a dataFrame in python pandas by two or more columns?

后端 未结 3 2061
青春惊慌失措
青春惊慌失措 2020-11-22 01:56

Suppose I have a dataframe with columns a, b and c, I want to sort the dataframe by column b in ascending order, and by c

3条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 02:28

    As of pandas 0.17.0, DataFrame.sort() is deprecated, and set to be removed in a future version of pandas. The way to sort a dataframe by its values is now is DataFrame.sort_values

    As such, the answer to your question would now be

    df.sort_values(['b', 'c'], ascending=[True, False], inplace=True)
    

提交回复
热议问题