如何在Pandas中对两个字段进行排序?

时间:2017-05-10 07:25:44

标签: python sorting pandas

我有以下数据类型:

id=["SA3342","ASD345","SFZX34","ASDFS5","SDFDS4","SFDDSF1"]
arrival_time = ["0"," 2016-05-19 13:50:00","2016-05-19 21:25:00","0","2016-05-24 18:30:00","2016-05-26 12:15:00"]
departure_time = ["2016-05-19 08:25:00","2016-05-19 16:00:00","2016-05-20 07:45:00","2016-05-24 12:50:00","2016-05-25 23:00:00","2016-05-26 19:45:00"]

获取以下数据:

id              arrival_time                departure_time
SA3342                 0                  2016-05-19 08:25:00
ASD345          2016-05-19 13:50:00       2016-05-19 16:00:00
SFZX34          2016-05-19 21:25:00       2016-05-20 07:45:00
ASDFS5                    0               2016-05-24 12:50:00
SDFDS4          2016-05-24 18:30:00       2016-05-25 23:00:00
SFDDSF1          2016-05-26 12:15:00       2016-05-26 19:45:00

如何根据“id”和出发时间对数据类型进行排序?

我尝试了以下代码但它没有用:

df = df.sort_values(['id', 'departure_time'], inplace=True, by='date')    

我收到以下错误:

TypeError: sort_values() got multiple values for keyword argument 'by'

1 个答案:

答案 0 :(得分:1)

您似乎需要删除if if inplace=True并删除sort_values中的第二个by

df.sort_values(by=['id', 'departure_time'], inplace=True)    
print (df)
        id          arrival_time       departure_time
1   ASD345   2016-05-19 13:50:00  2016-05-19 16:00:00
3   ASDFS5                     0  2016-05-24 12:50:00
0   SA3342                     0  2016-05-19 08:25:00
4   SDFDS4   2016-05-24 18:30:00  2016-05-25 23:00:00
5  SFDDSF1   2016-05-26 12:15:00  2016-05-26 19:45:00
2   SFZX34   2016-05-19 21:25:00  2016-05-20 07:45:00

或删除inplace=True和第二by

df = df.sort_values(by=['id', 'departure_time'])    
print (df)
        id          arrival_time       departure_time
1   ASD345   2016-05-19 13:50:00  2016-05-19 16:00:00
3   ASDFS5                     0  2016-05-24 12:50:00
0   SA3342                     0  2016-05-19 08:25:00
4   SDFDS4   2016-05-24 18:30:00  2016-05-25 23:00:00
5  SFDDSF1   2016-05-26 12:15:00  2016-05-26 19:45:00
2   SFZX34   2016-05-19 21:25:00  2016-05-20 07:45:00