python - 在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'
我对编码很陌生。将欣赏所有投入。
解决方案
推荐阅读
- r - 如何在 R Markdown 文档中间更改字体大小和类型?
- git - 在 git init 之后,Git 远程不显示 master 或 origin
- jquery - elm.children 不是函数(在 $('.input-group').each 内
- c++ - c++ srand(time(0)) 在猜谜游戏中不起作用
- ruby-on-rails - SecureRandom.hex() 与 SecureRandom.random_bytes() 在 Rails 中使用 attr_encrypted 生成密钥
- python - 将 VGGFace ResNet 导出到 TensorFlow Serving:ValueError:应定义“密集”输入的最后一个维度。找到`无`
- javascript - 在 Typescript 的类型注释中使用“is”关键字的目的是什么
- python - 在 Pandas 列中输入列表值
- github - 如何定义 GitHub 操作?
- javascript - HTML span 标签自动滚动