嘗試將 1 分鐘的 ohlc 轉(zhuǎn)換為 1 天的股票數(shù)據(jù)。我有兩個數(shù)據(jù)框df - 1 分鐘數(shù)據(jù)幀我正在嘗試制作的 df2- 1 天數(shù)據(jù)框df 有以下列cols = ["name", "date","time", "open", "high","low","close","sm","volume"]我正在像這樣初始化 df2df2 = pd.DataFrame( columns = ["name", "date", "open", "high","low","close"])df 的樣本數(shù)據(jù)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 分鐘數(shù)據(jù)。我想從 df 插入 1 天到 df2。對于初學(xué)者,我嘗試像這樣首先將當(dāng)天的開盤價和收盤價插入 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我應(yīng)該怎么辦?另外,考慮到 df 有整個月的 1 分鐘數(shù)據(jù),我怎樣才能獲得每天的最低價和最高價?
1 回答

郎朗坤
TA貢獻1921條經(jīng)驗 獲得超9個贊
您可以按 groupby ondate
然后聚合相關(guān)列。使用您的示例數(shù)據(jù),它可能是:
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
提交
取消