Python Pandas: Split a TimeSerie per month or week(Python Pandas:每月或每周拆分 TimeSerie)
问题描述
我有一个跨越几年的 Timeserie,格式如下:
I have a Timeserie that spans few year, in the following format:
timestamp open high low close volume
0 2009-01-02 05:00:00 900.00 906.75 898.00 904.75 15673.0
1 2009-01-02 05:30:00 904.75 907.75 903.75 905.50 4600.0
2 2009-01-02 06:00:00 905.50 907.25 904.50 904.50 3472.0
3 2009-01-02 06:30:00 904.50 905.00 903.25 904.75 6074.0
4 2009-01-02 07:00:00 904.75 905.50 897.00 898.25 12538.0
将该数据帧拆分为多个数据帧(包含 1 周或 1 个月的数据)的最简单方法是什么?77
What would be the simplest way to split that dataframe into multiple dataframes of 1 week or 1 month worth of data?77
例如,包含 1 年数据的数据帧将被拆分为 52 个包含一周数据的数据帧,并作为 52 个数据帧的列表返回
as an example a dataframe containing 1 year of data would be split in 52 dataframes containing a week of data and returned as a list of 52 dataframes
(数据可以用下面的公式重构)
(the data can be reconstructed with the formula below)
import pandas as pd
from pandas import Timestamp
dikt={'close': {0: 904.75, 1: 905.5, 2: 904.5, 3: 904.75, 4: 898.25}, 'low': {0: 898.0, 1: 903.75, 2: 904.5, 3: 903.25, 4: 897.0}, 'open': {0: 900.0, 1: 904.75, 2: 905.5, 3: 904.5, 4: 904.75}, 'high': {0: 906.75, 1: 907.75, 2: 907.25, 3: 905.0, 4: 905.5}, 'volume': {0: 15673.0, 1: 4600.0, 2: 3472.0, 3: 6074.0, 4: 12538.0}, 'timestamp': {0: Timestamp('2009-01-02 05:00:00'), 1: Timestamp('2009-01-02 05:30:00'), 2: Timestamp('2009-01-02 06:00:00'), 3: Timestamp('2009-01-02 06:30:00'), 4: Timestamp('2009-01-02 07:00:00')}}
pd.DataFrame(dikt, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
推荐答案
将 groupby
与 pd.TimeGrouper
和列表推导一起使用
use groupby
with pd.TimeGrouper
and list comprehensions
weeks = [g for n, g in df.set_index('timestamp').groupby(pd.TimeGrouper('W'))]
months = [g for n, g in df.set_index('timestamp').groupby(pd.TimeGrouper('M'))]
<小时>
如果需要,您可以重置索引
You can reset the index if you need
weeks = [g.reset_index()
for n, g in df.set_index('timestamp').groupby(pd.TimeGrouper('W'))]
months = [g.reset_index()
for n, g in df.set_index('timestamp').groupby(pd.TimeGrouper('M'))]
<小时>
在 dict
weeks = {n: g.reset_index()
for n, g in df.set_index('timestamp').groupby(pd.TimeGrouper('W'))}
months = {n: g.reset_index()
for n, g in df.set_index('timestamp').groupby(pd.TimeGrouper('M'))}
这篇关于Python Pandas:每月或每周拆分 TimeSerie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python Pandas:每月或每周拆分 TimeSerie


- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 沿轴计算直方图 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12