How to sort pandas data frame using values from several columns?

前端 未结 7 1164
走了就别回头了
走了就别回头了 2020-12-02 09:33

I have the following data frame:

df = pandas.DataFrame([{\'c1\':3,\'c2\':10},{\'c1\':2, \'c2\':30},{\'c1\':1,\'c2\':20},{\'c1\':2,\'c2\':15},{\'c1\':2,\'c2\'         


        
7条回答
  •  Happy的楠姐
    2020-12-02 10:30

    The dataframe.sort() method is - so my understanding - deprecated in pandas > 0.18. In order to solve your problem you should use dataframe.sort_values() instead:

    f.sort_values(by=["c1","c2"], ascending=[False, True])
    

    The output looks like this:

        c1  c2
        3   10
        2   15
        2   30
        2   100
        1   20
    

提交回复
热议问题