user7779326
user7779326

Reputation:

How to sort two fields in Pandas?

I have the following datatype:

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"]

To obtain the following data:

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

How to sort the datatype based on both "id" and departure time ?

I tried the following code and it didnt work:

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

I get the following error:

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

Upvotes: 2

Views: 175

Answers (1)

jezrael
jezrael

Reputation: 862591

It seems you need remove assign if parameter inplace=True and also remove second by in sort_values:

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

Or remove inplace=True and second 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

Upvotes: 1

Related Questions