0
我試圖將字典輸出轉換爲數據框。如何將字典轉換爲數據框
對於我的具體項目,我使用彭博服務API爲股票報價請求一些歷史數據點。他們給我的字典形式的輸出,但我需要將其轉換成更易於管理的DataFrame。到目前爲止,所有解決方案都顯得非常複雜有沒有一種簡單的pythonic方式來完成這件事?
謝謝!
的代碼生成輸出的片段:
def main():
output = {}
options = parseCmdLine()
# Fill SessionOptions
sessionOptions = blpapi.SessionOptions()
sessionOptions.setServerHost(options.host)
sessionOptions.setServerPort(options.port)
print ("Connecting to %s:%s" % (options.host, options.port))
# Create a Session
session = blpapi.Session(sessionOptions)
# Start a Session
if not session.start():
print ("Failed to start session.")
return
try:
# Open service to get historical data from
if not session.openService("//blp/refdata"):
print ("Failed to open //blp/refdata")
return
# Obtain previously opened service
refDataService = session.getService("//blp/refdata")
# Create and fill the request for the historical data
request = refDataService.createRequest("HistoricalDataRequest")
request.getElement("fields").appendValue("BEST_SALES")
request.getElement("fields").appendValue("BEST_EBITDA")
request.getElement("fields").appendValue("BEST_EPS")
request.getElement("fields").appendValue("CURR_ENTP_VAL")
request.getElement("fields").appendValue("CUR_MKT_CAP")
request.getElement("fields").appendValue("LAST_PRICE")
# Elements passed to it
request.getElement("securities").appendValue("MSFT US Equity")
# Add overrides
overrides = request.getElement("overrides")
override1 = overrides.appendElement()
override1.setElement("fieldId", "BEST_FPERIOD_OVERRIDE")
override1.setElement("value", "1FY")
override2 = overrides.appendElement()
override2.setElement("fieldId", "BEST_CONSOLIDATED_OVERRIDE")
override2.setElement("value", "C")
override3 = overrides.appendElement()
override3.setElement("fieldId", "EQY_FUND_CRNCY")
override3.setElement("value", "USD")
# Add historical adjustments
request.set("periodicityAdjustment", "ACTUAL")
request.set("periodicitySelection", "DAILY")
request.set("maxDataPoints", 100)
## NEED TO PASS THIS
request.set("startDate", "20160106")
request.set("endDate", "20160107")
print ("Sending Request:", request)
# Send the request
session.sendRequest(request)
# Process received events
while(True):
# We provide timeout to give the chance for Ctrl+C handling:
ev = session.nextEvent(500)
for msg in ev:
print (msg)
if ev.eventType() == blpapi.Event.RESPONSE:
# Response completely received, so we could exit
break
finally:
# Stop the session
session.stop()
輸出在字典形式:
HistoricalDataResponse = {
securityData = {
security = "MSFT US Equity"
eidData[] = {
}
sequenceNumber = 0
fieldExceptions[] = {
}
fieldData[] = {
fieldData = {
date = 2016-01-06
BEST_SALES = 98338.750000
BEST_EPS = 3.108000
CURR_ENTP_VAL = 373535.702300
CUR_MKT_CAP = 431746.702300
LAST_PRICE = 54.050000
}
fieldData = {
date = 2016-01-07
BEST_SALES = 98351.040000
BEST_EBITDA = 37885.200000
BEST_EPS = 3.110000
CURR_ENTP_VAL = 358518.425700
CUR_MKT_CAP = 416729.425700
LAST_PRICE = 52.170000
}
}
}
}
這不是一個有效的Python文字...請嘗試提供一個可重複的示例。 –