在 pandas 数据框中查找出现次数最多的 timedelta 小时

Find timedelta hour with highest number of occurences in pandas dataframe

我有一个数据框,用于存储订单和收到订单的时间

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

可能有订单,但数据框包含当月的订单。我想知道这个月哪个小时收到的订单最多。我尝试创建一个这样的系列。

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

这样我就可以像这样按小时分组

orders.groupby(orders.index.hour)

但这没有意义,因为我想获得我收到最多订单的时间。我将如何实现这一目标?

I want to get the hour where I receive the most orders

这里很好用 Series.value_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

所以对于热门时段 select 第一个索引值:

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

对于最高值 select Series 的第一个值:

no = s.iat[0]
print (no)
3