How to sort pandas data frame using values from several columns?
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 "<stdin>", line 1, in <module>
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'
Use of sort
can result in warning message. See github discussion.
So you might wanna use sort_values
, docs here
Then your code can look like this:
df = df.sort_values(by=['c1','c2'], ascending=[False,True])
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
In my case, the accepted answer didn't work:
f.sort_values(by=["c1","c2"], ascending=[False, True])
Only the following worked as expected:
f = f.sort_values(by=["c1","c2"], ascending=[False, True])