首页 > 解决方案 > 在python中根据名称和时间聚合数据

问题描述

我有一组每分钟记录的大约 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  

            

例如,“28707 Neumü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'

我对编码很陌生。将欣赏所有投入。

标签: pythonpandastimestamp

解决方案


推荐阅读