使用 Python Pandas 对 csv 文件中的行进行排序 [英] Sorting rows in csv file using Python Pandas

查看:166
本文介绍了使用 Python Pandas 对 csv 文件中的行进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于使用 Pandas 对 csv 文件中的行进行排序的快速问题.我拥有的 csv 文件的数据如下所示:

季度周值5 1 2003 2 1002 1 502 2 1254 2 1752 3 1953 1 105 2 190

我需要按以下方式排序:对季度和相应的周进行排序.所以输出应该如下所示:

季度周值2 1 502 2 1252 3 1953 1 103 2 1004 2 1755 1 2005 2 190

我的尝试:

df = df.sort('季度', '周')

但这不会产生正确的结果.任何帮助/建议?

谢谢!

解决方案

注意: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

这篇关于使用 Python Pandas 对 csv 文件中的行进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆