Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting rows in csv file using Python Pandas

Tags:

python

pandas

csv

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!

like image 524
Prakhar Mehrotra Avatar asked Dec 21 '22 02:12

Prakhar Mehrotra


2 Answers

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

like image 125
Computer's Guy Avatar answered Dec 28 '22 22:12

Computer's Guy


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
like image 27
DSM Avatar answered Dec 28 '22 23:12

DSM