且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Python Pandas 对 csv 文件中的行进行排序

更新时间:2023-01-20 17:09:50

注意:sort 已被弃用,取而代之的是 sort_values,你应该在 Pandas 0.17+ 中使用.

输入 help(df.sort) 给出:

sort(self, columns=None, column=None, axis=0, Ascending=True, inplace=False) pandas.core.frame.DataFrame 实例的方法按标签(沿任一轴)或中的值对 DataFrame 进行排序列)参数----------列:对象框架中的列名.接受列名或列表或元组对于嵌套排序.[...]例子-------->>>结果 = df.sort(['A', 'B'], 升序=[1, 0])[...]

因此您将要排序的列作为列表传递:

>>>df季度价值0 5 1 2001 3 2 1002 2 1 503 2 2 1254 4 2 1755 2 3 1956 3 1 107 5 2 190>>>df.sort(["季度","周"])季度价值2 2 1 503 2 2 1255 2 3 1956 3 1 101 3 2 1004 4 2 1750 5 1 2007 5 2 190

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!

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