I have a dataframe where I store orders and the time at which they are received
JavaScript
x
7
1
order_id time_placed
2
A1 2019-08-01 06:09:55.670712
3
A2 2019-08-01 06:09:55.687803
4
A3 2019-08-01 07:27:21.236759
5
A4 2019-08-01 07:27:21.256607
6
A5 2019-08-01 07:27:21.272751
7
There are may orders but the dataframe contains orders for the month. I want to know which hour I receive the most orders during the month. I tried creating a series like this.
JavaScript
1
2
1
orders = pd.Series(order_list['order_id'].tolist(), index=order_list['time_placed'])
2
So that I could group by hour like this
JavaScript
1
2
1
orders.groupby(orders.index.hour)
2
But it doesn’t make sense because I want to get the hour where I receive the most orders. How would I achieve this?
Advertisement
Answer
I want to get the hour where I receive the most orders
Here is nice use Series.value_counts
, because by default sorting by counts.
JavaScript
1
8
1
df['time_placed'] = pd.to_datetime(df['time_placed'])
2
3
s = df.time_placed.dt.hour.value_counts()
4
print (s)
5
7 3
6
6 2
7
Name: time_placed, dtype: int64
8
So for top hour select first index value:
JavaScript
1
4
1
h = s.index[0]
2
print (h)
3
7
4
And for top value select first value of Series
:
JavaScript
1
4
1
no = s.iat[0]
2
print (no)
3
3
4