我正在尝试使用 pandas 将一堆文件的 1M OHLC 转换为 5M OHLC
这就是我的数据的样子:
dateTime | open | high | low | close | vol |
-----------------------------------------------------------
01-06-2018 00:50:00 | 0.97456| 0.2456|0.2145|0.241|54.26
01-06-2018 00:51:00 | 0.94566| 0.2145|0.1455|0.214|65.24
01-06-2018 00:52:00 | 0.89654| 0.2145|0.2144|0.214|73.25
如何重新采样并保存为 5M OHLC csv
提前致谢
编辑1:这是我通过 print (df.info()) 得到的结果
<class 'pandas.core.frame.DataFrame'>
Index: 375660 entries, 2018-06-01 00:00:00 to 2019-05-31 20:59:00
Data columns (total 4 columns):
open 375660 non-null float64
high 375660 non-null float64
low 375660 non-null float64
close 375660 non-null float64
dtypes: float64(4)
memory usage: 14.3+ MB
None
最佳答案
将 Resampler.agg
与字典一起使用,以获取列名称,并使用 5T
进行聚合 5 分钟
:
d = {'open':'first', 'high':'max','low':'min','close':'last','vol':'sum'}
df['dateTime'] = pd.to_datetime(df['dateTime'])
df = df.resample('5T', on='dateTime').agg(d)
print (df)
open high low close vol
dateTime
2018-01-06 00:50:00 0.97456 0.2456 0.1455 0.214 192.75
关于python - 如何使用 pandas 将 1M OHLC 转换为 5M OHLC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57412414/