Sort_values() With Key In Python
I have a dataframe where the column names are times (0:00, 0:10, 0:20, ..., 23:50). Right now, they're sorted in a string order (so 0:00 is first and 9:50 is last) but I want to so
Solution 1:
Try sorting the columns with the sorted
builtin function and passing the output to the dataframe for indexing. The following should serve as a working example:
import pandas as pd
records = [(2, 33, 23, 45), (3, 4, 2, 4), (4, 5, 7, 19), (4, 6, 71, 2)]
df = pd.DataFrame.from_records(records, columns = ('0:00', '23:40', '12:30', '11:23'))
df# 0:00 23:40 12:30 11:23# 0 2 33 23 45# 1 3 4 2 4# 2 4 5 7 19# 3 4 6 71 2df[sorted(df,key=pd.to_datetime)]
# 0:00 11:23 12:30 23:40# 0 2 45 23 33# 1 3 4 2 4# 2 4 19 7 5# 3 4 2 71 6
I hope this helps
Solution 2:
Just prepend a leading zero to one-digit hours. This should be the simplest solution as you can simply sort lexically then.
E.g. 5:30 -> 05:30.
Solution 3:
Here is a working demo, which implements @MartinKrämer's idea:
import re
In [259]: df
Out[259]:
23:400:0019:1912:30 09:0011:230332123124514312134254171419364171142
In [260]: df.rename(columns=lambda x: re.sub(r'^(\d{1})\:', r'0\1:', x)).sort_index(axis=1)
Out[260]:
00:00 09:0011:2312:3019:1923:400212452313313134214241419715341427116
Solution 4:
I know this question is a few years old, but since it's the top Google result for this question, I wanted to provide the root cause of the error.
The 'key' argument was added to sort_values in version 1.1.0. See the note in the documentation linked below.
This feature will very like work as you intended if you upgrade to 1.1.0 or higher.
Post a Comment for "Sort_values() With Key In Python"