5

I have a quick question regarding sorting rows in a csv files using Pandas. The csv file which I have has the data that looks like:

quarter week    Value
  5       1      200   
  3       2      100
  2       1       50
  2       2      125
  4       2      175 
  2       3      195 
  3       1      10
  5       2      190

I need to sort in following way: sort the quarter and the corresponding weeks. So the output should look like following:

quarter week    Value
  2       1      50  
  2       2      125
  2       3      195
  3       1      10
  3       2      100    
  4       2      175
  5       1      200
  5       2      190

My attempt:

df = df.sort('quarter', 'week') 

But this does not produce the correct result. Any help/suggestions?

Thanks!

3 Answers 3

10

New answer, as of 14 March 2019

df.sort_values(by=["COLUMN"], ascending=False)

This returns a new sorted data frame, doesn't update the original one.

Note: You can change the ascending parameter according to your needs, without passing it, it will default to ascending=True

Sign up to request clarification or add additional context in comments.

1 Comment

doesnt help me, on CSV it says: 102.314 little than 106, how can it be possible?
5

Note: sort has been deprecated in favour of sort_values, which you should use in Pandas 0.17+.

Typing help(df.sort) gives:

sort(self, columns=None, column=None, axis=0, ascending=True, inplace=False) method of pandas.core.frame.DataFrame instance
    Sort DataFrame either by labels (along either axis) or by the values in
    column(s)

    Parameters
    ----------
    columns : object
        Column name(s) in frame. Accepts a column name or a list or tuple
        for a nested sort.

[...]

Examples
--------
>>> result = df.sort(['A', 'B'], ascending=[1, 0])

[...]

and so you pass the columns you want to sort as a list:

>>> df
   quarter  week  Value
0        5     1    200
1        3     2    100
2        2     1     50
3        2     2    125
4        4     2    175
5        2     3    195
6        3     1     10
7        5     2    190
>>> df.sort(["quarter", "week"])
   quarter  week  Value
2        2     1     50
3        2     2    125
5        2     3    195
6        3     1     10
1        3     2    100
4        4     2    175
0        5     1    200
7        5     2    190

1 Comment

I know this answer was given four years ago, but thought I would add that df.sort(['A', 'B']) is now deprecated. The new method is df.sort_values(by=['A', 'B']).
-1

DataFrame object has no attribute sort

1 Comment

Thank you for contributing, but this answer could be greatly improved! You have just stated the reason why that method does not work, but didn't provide an actual solution for the problem. Please be a bit more verbose and try to answer the original question! (Also, posting a short answer to an old question which already has a solution is not usually useful.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.