Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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

1 Answer

0 votes
by (41.4k points)

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'

Browse Categories

...