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

前端 未结 7 1172
走了就别回头了
走了就别回头了 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条回答
  •  星月不相逢
    2020-12-02 10:21

    DataFrame.sort is deprecated; use DataFrame.sort_values.

    >>> df.sort_values(['c1','c2'], ascending=[False,True])
       c1   c2
    0   3   10
    3   2   15
    1   2   30
    4   2  100
    2   1   20
    >>> df.sort(['c1','c2'], ascending=[False,True])
    Traceback (most recent call last):
      File "", line 1, in 
      File "/Users/ampawake/anaconda/envs/pseudo/lib/python2.7/site-packages/pandas/core/generic.py", line 3614, in __getattr__
        return object.__getattribute__(self, name)
    AttributeError: 'DataFrame' object has no attribute 'sort'
    

提交回复
热议问题