【问题标题】:Count the average number per day计算每天的平均人数
【发布时间】:2020-01-14 02:02:44
【问题描述】:

我需要计算每天出现的平均数据点数。但我不知道如何在 python 中为它编写代码。下面的数据是数据外观的示例。它是一个 ndarray 并使用 panda 日期时间。我得到的预期值是 01-01 每天有 2 个,01-02 每天有 1 个,01-03 每天有 2 个。

   temp time =  array([ Timestamp('1979-01-01 11:21:59.904000'),
   Timestamp('1979-01-01 19:59:00.096000'),
   Timestamp('1979-01-02 07:54:59.904000'),
   Timestamp('1979-01-03 01:03:00'),
   Timestamp('1979-01-03 07:41:59.712000')]

【问题讨论】:

  • The data below is an example of what the data looks like,你能更准确一点吗?这是一个数据框吗?
  • @yatu 这是一个 ndarray
  • @yatu 熊猫数据时间

标签: python


【解决方案1】:

如果我没听错的话,你想使用pd.Grouper,频率设置为'D'

例如:

time = np.array([pd.Timestamp('1979-01-01 11:21:59.904000'),
   pd.Timestamp('1979-01-01 19:59:00.096000'),
   pd.Timestamp('1979-01-02 07:54:59.904000'),
   pd.Timestamp('1979-01-03 01:03:00'),
   pd.Timestamp('1979-01-03 07:41:59.712000')])

df = pd.DataFrame({'time':time})

print( df.groupby(pd.Grouper(key='time', freq='D'))['time'].count() )

打印:

time
1979-01-01    2
1979-01-02    1
1979-01-03    2
Freq: D, Name: time, dtype: int64

【讨论】:

  • @WoodyTheCoder 第二个'time' 用于您想要的系列应用.count() 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 2018-11-29
  • 2022-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多