猿问

如何将 DatetimeIndexResamplerGroupby 对象转换为数据框?

我想以 30 秒的间隔对具有时间序列数据的数据帧重新采样到 1 秒的间隔。为此,我使用了:

test_data=test_data.groupby('entity_id').resample('S', fill_method='ffill')

输出是: <pandas.core.resample.DatetimeIndexResamplerGroupby object at 0x1a1f64f588>如何将此对象转换为数据框?

我尝试过: test_data = pd.DataFrame(test_data) 在运行最后一个命令后,它返回一个数据框,其中包含该行的索引和所有其他元素的列表。


元芳怎么了
浏览 119回答 1
1回答

交互式爱情

使用ffill方法:test_data = pd.DataFrame({&nbsp; &nbsp; 'entity_id': ['a','a','a','a','b','b','b','c','d'],&nbsp; &nbsp; 'data':range(9)},&nbsp;&nbsp; &nbsp; &nbsp;index=pd.date_range('2018-01-01', periods=9, freq='3S'))print (test_data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entity_id&nbsp; data2018-01-01 00:00:00&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp;02018-01-01 00:00:03&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp;12018-01-01 00:00:06&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp;22018-01-01 00:00:09&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp;32018-01-01 00:00:12&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b&nbsp; &nbsp; &nbsp;42018-01-01 00:00:15&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b&nbsp; &nbsp; &nbsp;52018-01-01 00:00:18&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b&nbsp; &nbsp; &nbsp;62018-01-01 00:00:21&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c&nbsp; &nbsp; &nbsp;72018-01-01 00:00:24&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d&nbsp; &nbsp; &nbsp;8test_data=test_data.groupby('entity_id')['data'].resample('S').ffill()print (test_data)entity_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2018-01-01 00:00:00&nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:01&nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:02&nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:03&nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:04&nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:05&nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:06&nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:07&nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:08&nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:09&nbsp; &nbsp; 3b&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2018-01-01 00:00:12&nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:13&nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:14&nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:15&nbsp; &nbsp; 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:16&nbsp; &nbsp; 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:17&nbsp; &nbsp; 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-01 00:00:18&nbsp; &nbsp; 6c&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2018-01-01 00:00:21&nbsp; &nbsp; 7d&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2018-01-01 00:00:24&nbsp; &nbsp; 8Name: data, dtype: int64
随时随地看视频慕课网APP

相关分类

Python
我要回答