我已經安裝了Bloomberg API和pdblp庫。我能夠獲取歷史數據並將其存儲在Dataframe中。但我不確定如何從多級數據框訪問數據。如何在Python中訪問多級熊貓數據框 - 在數據框中存儲彭博數據
import pdblp
import pandas as pd
con = pdblp.BCon(debug=True, port=8194)
con.start()
start = datetime.datetime.strptime("19800101", '%Y%m%d').strftime("%Y%m%d")
end = datetime.date.today().strftime("%Y%m%d")
df = pd.DataFrame(con.bdh('SPY US Equity',['PX_LAST', 'VOLUME'],start, end))
print(df)
我無法訪問Dataframe中的日期列。誰能幫幫我嗎。 如果我嘗試 - df.columns我得到以下的輸出:
MultiIndex(levels=[['SPY US Equity'], ['PX_LAST', 'VOLUME']],
labels=[[0, 0], [0, 1]],
names=['ticker', 'field'])
下面是從數據幀數據
ticker SPY US Equity
field PX_LAST VOLUME
date
1993-01-29 43.9375 1003200.0
1993-02-01 44.2500 480500.0
1993-02-02 44.3438 201300.0
1993-02-03 44.8125 529400.0
1993-02-04 45.0000 531500.0
1993-02-05 44.9688 492100.0
1993-02-08 44.9688 596100.0
1993-02-09 44.6563 122100.0
1993-02-10 44.7188 379600.0
1993-02-11 44.9375 19500.0
1993-02-12 44.5938 42500.0
1993-02-16 43.4688 374800.0
1993-02-17 43.4375 210900.0
1993-02-18 43.4063 378100.0
1993-02-19 43.5625 34900.0
1993-02-22 43.7188 513600.0
1993-02-23 43.6875 373700.0
1993-02-24 44.2500 26300.0
1993-02-25 44.3438 44500.0
1993-02-26 44.4063 66200.0
1993-03-01 44.2813 66500.0
1993-03-02 44.9375 182400.0
df.index,給出以下結果:
DatetimeIndex(['1993-01-29', '1993-02-01', '1993-02-02', '1993-02-03',
'1993-02-04', '1993-02-05', '1993-02-08', '1993-02-09',
'1993-02-10', '1993-02-11',
...
'2017-07-13', '2017-07-14', '2017-07-17', '2017-07-18',
'2017-07-19', '2017-07-20', '2017-07-21', '2017-07-24',
'2017-07-25', '2017-07-26'],
dtype='datetime64[ns]', name='date', length=6168, freq=None)
DF 'loc ['1993-02-22']得出如下結果:
ticker field
SPY US Equity PX_LAST 43.7188
VOLUME 513600.0000
是什麼'df.index'顯示:現在
,您可以在 '日期' 欄用看?它看起來像'date'是你的索引,如果是'df.loc ['1993-02-22']'作爲例子 – EdChum
用df.index和df.loc更新了這個問題['1993-02-22' ]結果 –