Reputation: 47
I have concatenated several csv files
into one dataframe
to make a combined csv file
. But one of the columns
has both date
and time
(e.g 02:33:01 21-Jun-2018)
after being converted to date_time
format. However when I call
new_dataframe = old_dataframe.sort_values(by = 'Time')
It sorts the dataframe by time , completely ignoring date.
Index Time Depth(ft) Pit Vol(bbl) Trip Tank(bbl)
189147 00:00:00 03-May-2018 2283.3578 719.6753 54.2079
3875 00:00:00 07-May-2018 5294.7308 1338.7178 29.5781
233308 00:00:00 20-May-2018 8073.7988 630.7964 41.3574
161789 00:00:01 05-May-2018 122.2710 353.6866 58.9652
97665 00:00:01 01-May-2018 16178.8666 769.1328 66.0688
How do I get it to sort by dates and then times , so that Aprils
days come first, and come in chronological order
?
Upvotes: 0
Views: 477
Reputation: 34046
In order to sort with your date
first and then time
, your Time
column should be in the right way Date followed by Time
. Currently, it's opposite.
You can do this:
df['Time'] = df['Time'].str.split(' ').str[::-1].apply(lambda x: ' '.join(x))
df['Time'] = pd.to_datetime(df['Time'])
Now sort your df by Time
like this:
df.sort_values('Time')
Upvotes: 2