Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort date by descending order and time by ascending order using Pandas

My df looks like this. It is an hourly dataset.

time                    Open
2017-01-03 09:00:00     5.2475
2017-01-03 08:00:00     5.2180
2017-01-03 07:00:00     5.2128
2017-01-02 09:00:00     5.4122
2017-01-02 08:00:00     5.2123
2017-01-02 07:00:00     5.2475
2017-01-01 09:00:00     5.2180
2017-01-01 08:00:00     5.2128
2017-01-01 07:00:00     5.4122

I want to sort the hourly only data by ascending order.

What did I do?

I did:

df.sort_values(by='time', ascending=True)

but it sort entire value of time however I want to only sort the time section.

My new df should look like this:

time                    Open
2017-01-03 07:00:00     5.2475
2017-01-03 08:00:00     5.2180
2017-01-03 09:00:00     5.2128
2017-01-02 07:00:00     5.4122
2017-01-02 08:00:00     5.2123
2017-01-02 09:00:00     5.2475
2017-01-01 07:00:00     5.2180
2017-01-01 08:00:00     5.2128
2017-01-01 09:00:00     5.4122

Here date stays the same but the time is in ascending order.

like image 755
floss Avatar asked Mar 03 '23 15:03

floss


1 Answers

If need sorting by dates and times create new columns for sorting by DataFrame.assign, then sort by both columns with DataFrame.sort_values and ascending parameter, because sorting by dates is descending and by times is ascending and last remove helper columns with DataFrame.drop:

df1 = (df.assign(d=df['time'].dt.date,
                 t=df['time'].dt.time)
         .sort_values(['d','t'], ascending=[False, True])
         .drop(['d','t'], axis=1))
print (df1)
                 time    Open
2 2017-01-03 07:00:00  5.2128
1 2017-01-03 08:00:00  5.2180
0 2017-01-03 09:00:00  5.2475
5 2017-01-02 07:00:00  5.2475
4 2017-01-02 08:00:00  5.2123
3 2017-01-02 09:00:00  5.4122
8 2017-01-01 07:00:00  5.4122
7 2017-01-01 08:00:00  5.2128
6 2017-01-01 09:00:00  5.2180

Or if dates cannot be changed and need sorting only by times use DataFrame.groupby with lambda function - groupby not sorting, because sort=False parameter and group_keys=False is for avoid MultiIndex:

df1 = (df.groupby(df['time'].dt.date, sort=False, group_keys=False)
         .apply(lambda x: x.sort_values('time')))
print (df1)
                 time    Open
2 2017-01-03 07:00:00  5.2128
1 2017-01-03 08:00:00  5.2180
0 2017-01-03 09:00:00  5.2475
5 2017-01-02 07:00:00  5.2475
4 2017-01-02 08:00:00  5.2123
3 2017-01-02 09:00:00  5.4122
8 2017-01-01 07:00:00  5.4122
7 2017-01-01 08:00:00  5.2128
6 2017-01-01 09:00:00  5.2180
like image 197
jezrael Avatar answered Apr 29 '23 04:04

jezrael