尝试将 1 分钟的 ohlc 转换为 1 天的股票数据。我有两个数据框df - 1 分钟数据帧我正在尝试制作的 df2- 1 天数据框df 有以下列cols = ["name", "date","time", "open", "high","low","close","sm","volume"]我正在像这样初始化 df2df2 = pd.DataFrame( columns = ["name", "date", "open", "high","low","close"])df 的样本数据name date time open high low close sm volumeBANKNIFTY_F1 20190101 09:16 27300.2 27368.1 27300.2 27314.9 18300 1414860BANKNIFTY_F1 20190101 09:17 27317.7 27322.1 27289.9 27299.3 11980 1414860BANKNIFTY_F1 20190101 09:18 27298.6 27309.0 27296.2 27307.0 10760 1414860BANKNIFTY_F1 20190101 09:19 27302.7 27308.7 27277.0 27282.0 13620 1424420BANKNIFTY_F1 20190101 09:20 27282.0 27282.0 27257.5 27264.4 16040 1424420就像这个 df 有整个月的 1 分钟数据。我想从 df 插入 1 天到 df2。对于初学者,我尝试像这样首先将当天的开盘价和收盘价插入 df2for j in range(df['date'].nunique()): #open and close for i in range(len(df)): #open if(df['time'][i]=='09:16'): df2['open'][j]=df['open'][0] #close if(df['time'][i]=='15:30'): df2['close'][j]=df['close'][i]这让我返回错误File "<ipython-input-26-43c7b22fee98>", line 1, in <module> df2['open'][0]=df['open'][0] File "/home/devang/anaconda3/lib/python3.7/site-packages/pandas/core/series.py", line 1020, in __setitem__ values[key] = valueIndexError: index 0 is out of bounds for axis 0 with size 0我应该怎么办?另外,考虑到 df 有整个月的 1 分钟数据,我怎样才能获得每天的最低价和最高价?
1 回答
郎朗坤
TA贡献1921条经验 获得超9个赞
您可以按 groupby ondate
然后聚合相关列。使用您的示例数据,它可能是:
df.groupby('date').agg({'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last'})
给予:
open high low close date 20190101 27300.2 27368.1 27257.5 27264.4
添加回答
举报
0/150
提交
取消