Skip to content
Advertisement

Find timedelta hour with highest number of occurences in pandas dataframe

I have a dataframe where I store orders and the time at which they are received

order_id   time_placed
A1         2019-08-01 06:09:55.670712
A2         2019-08-01 06:09:55.687803
A3         2019-08-01 07:27:21.236759
A4         2019-08-01 07:27:21.256607
A5         2019-08-01 07:27:21.272751

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.

  orders = pd.Series(order_list['order_id'].tolist(), index=order_list['time_placed'])

So that I could group by hour like this

orders.groupby(orders.index.hour)

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.

df['time_placed'] = pd.to_datetime(df['time_placed'])

s = df.time_placed.dt.hour.value_counts()
print (s)
7    3
6    2
Name: time_placed, dtype: int64

So for top hour select first index value:

h = s.index[0]
print (h)
7

And for top value select first value of Series:

no = s.iat[0]
print (no)
3
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement