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':100}])
Or, in human readable form:
c1 c2 0 3 10 1 2 30 2 1 20 3 2 15 4 2 100
The following sorting-command works as expected:
df.sort(['c1','c2'], ascending=False)
Output:
c1 c2 0 3 10 4 2 100 1 2 30 3 2 15 2 1 20
But the following command:
df.sort(['c1','c2'], ascending=[False,True])
results in
c1 c2 2 1 20 3 2 15 1 2 30 4 2 100 0 3 10
and this is not what I expect. I expect to have the values in the first column ordered from largest to smallest, and if there are identical values in the first column, order by the ascending values from the second column.
Does anybody know why it does not work as expected?
ADDED
This is copy-paste:
>>> df.sort(['c1','c2'], ascending=[False,True]) c1 c2 2 1 20 3 2 15 1 2 30 4 2 100 0 3 10
Sorting Your DataFrame on a Single Column. To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With