使用 Python Pandas 对 csv 文件中的行进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/17870476/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting rows in csv file using Python Pandas
提问by Prakhar Mehrotra
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:
我有一个关于使用 Pandas 对 csv 文件中的行进行排序的快速问题。我拥有的 csv 文件的数据如下:
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!
谢谢!
回答by DSM
Note:
sorthas been deprecated in favour ofsort_values, which you should use in Pandas 0.17+.
注意:
sort已被弃用sort_values,您应该在 Pandas 0.17+ 中使用它。
Typing help(df.sort)gives:
打字help(df.sort)给出:
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
回答by octohedron
New answer, as of 14 March 2019
新答案,截至 2019 年 3 月 14 日
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
注:可以根据自己的需要更改升序参数,不传则默认为 ascending=True
回答by Georgi Ivanov Dimitrov
DataFrameobject has no attribute sort
DataFrame对象没有属性 sort

