根据名称和时间在python中聚合数据

时间:2020-06-22 14:26:25

标签: python pandas timestamp

我每分钟记录约200个自行车站的数据。

我正在尝试汇总数据,以便以15分钟的间隔获得每个站点的结果。

数据现在看起来像这样

          Description                                          timestamp             BikeAvailable  
         28707 Neumühlen / Övelgönne                         2019-12-16 13:38:05          False  
         28703 Fischersallee / Bleickenallee                 2019-12-16 13:38:05          False  
         28702 Bleickenallee / Kinderkrankenhaus Altona      2019-12-16 13:38:05           True      
         28704 Eulenstraße / Große Brunnenstraße             2019-12-16 13:38:05          False  
         28705 Große Rainstraße/Ottenser Hauptstraße         2019-12-16 13:38:05           True  

            

例如,“ 28707Neumühlen/Övelgönne”将成为车站的名称。

我目前正在使用

dftest = df.groupby(['timestamp', 'Description'])

将变量分组并希望最终使用

dftestR = dftest.resample('15min').last()

要对我的数据进行采样,但是我目前不知道如何到达那里,我应该首先按每个电台名称对数据进行子集化吗?

运行“ .groupby”行后,我不仅只能对数据运行“ .resample”行并得到TypeError。似乎我的数据不再被视为日期时间索引。我收到以下错误消息:

 Traceback (most recent call last):
  File "G:\Conda3\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-12-68aebddc2221>", line 2, in <module>
    dftestR = dftest.resample('15min').last()
  File "G:\Conda3\lib\site-packages\pandas\core\groupby\groupby.py", line 1565, in resample
    return get_resampler_for_grouping(self, rule, *args, **kwargs)
  File "G:\Conda3\lib\site-packages\pandas\core\resample.py", line 1287, in get_resampler_for_grouping
    resampler = tg._get_resampler(groupby.obj, kind=kind)
  File "G:\Conda3\lib\site-packages\pandas\core\resample.py", line 1404, in _get_resampler
    "Only valid with DatetimeIndex, "
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'

对于编码非常陌生。将感谢所有输入。

0 个答案:

没有答案