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 sort them after time (so 0:00 is first and 23:50 is last).
If time is a column, you can use
df = df.sort(columns='Time',key=float)
But 1) that only works if time is a column itself, rather than the column names, and 2) sort() is deprecated so I try to abstain from using it.
I'm trying to use
df = df.sort_index(axis = 1)
but since the column names are in string format, they get sorted according to a string key. I've tried
df = df.sort_index(key=float, axis=1)
but that gives an error message:
Traceback (most recent call last):
File "<ipython-input-112-5663f277da66>", line 1, in <module>
df.sort_index(key=float, axis=1)
TypeError: sort_index() got an unexpected keyword argument 'key'
Does anyone have ideas for how to fix this? So annoying that sort_index() - and sort_values() for that matter - don't have the key argument!!
df[sorted(df,key=pd.to_datetime)]should do.1-01-01 00:00:00is clearly not a date or time.