根据两列组对所有记录进行排序

时间:2018-01-31 02:14:19

标签: python pandas sorting

我有这样的数据集:

user_id     record_time               accu       lat       lon        bin-lat  bin-lon
0   1335    2017-02-28 18:11:00     21.581  52.130910   -106.639214      207    185     
1   1335    2017-02-28 18:17:00     21.205  52.130910   -106.639214      208    183     
2   1335    2017-02-28 18:22:00     21.998  52.131086   -106.638881      207    185     
3   1335    2017-02-28 18:25:00     28.000  52.130828   -106.637958      209    184     
4   1335    2017-02-28 18:25:00     7.000   52.130806   -106.637825      208    183     
5   1335    2017-02-28 18:25:00     6.000   52.130799   -106.637810      209    184     

我想基于两列(' bin-lat'' bin-lon')的组对整个数据集进行排序。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

如果你将它放入pandas数据帧,你可以按列进行简单的排序:

import os
import sys


def pyh_new( filename ):

    if ( os.path.isfile( filename ) ):

        file = open( filename, 'r' )
        line_list = file.readlines()
        file.close()

def pyh_new( filename ):

    if ( os.path.isfile( filename ) ):

        file = open( filename, 'w' )
        line_list = file.writelines()
        #need to write 

        file.close()



if __name__ == '__main__':

    pyh_new( "pyh.py" )

答案 1 :(得分:1)

>>> student_objects = [
...     Student('john', 'A', 15),
...     Student('jane', 'B', 12),
...     Student('dave', 'B', 10),
... ]
>>> s = sorted(student_objects, key=attrgetter('age'))     # sort on secondary key
>>> sorted(s, key=attrgetter('grade'), reverse=True)       # now sort on primary key, descending
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

参考: https://docs.python.org/3/howto/sorting.html#sort-stability-and-complex-sorts