首页 > 解决方案 > 将包含多个对象的pandas对象转换为单个pandas数据框的过程是什么

问题描述

使用 Interactive Brokers API,我的数据有时需要转换为简单的二维数据框。以下是从 API 返回的 Interactive Brokers 数据示例。为了让 Stack Overflow 用户能够看到实际数据,我尝试将其置于易于检索的形式中:

import pandas as pd
import numpy as np

df_dict = df.to_dict()
print (df_dict)

[5 rows x 5 columns]
{'contract': {13: Future(conId=357283192, symbol='NQ', lastTradeDateOrContractMonth='20200619', right='?', multiplier='20', exchange='GLOBEX', currency='USD', localSymbol='NQM0', tradingClass='NQ'), 14: Future(conId=357283192, symbol='NQ', lastTradeDateOrContractMonth='20200619', right='?', multiplier='20', exchange='GLOBEX', currency='USD', localSymbol='NQM0', tradingClass='NQ'), 15: Future(conId=357283192, symbol='NQ', lastTradeDateOrContractMonth='20200619', right='?', multiplier='20', exchange='GLOBEX', currency='USD', localSymbol='NQM0', tradingClass='NQ'), 16: Future(conId=357283192, symbol='NQ', lastTradeDateOrContractMonth='20200619', right='?', multiplier='20', exchange='GLOBEX', currency='USD', localSymbol='NQM0', tradingClass='NQ'), 17: Future(conId=357283192, symbol='NQ', lastTradeDateOrContractMonth='20200619', right='?', multiplier='20', exchange='GLOBEX', currency='USD', localSymbol='NQM0', tradingClass='NQ')}, 'order': {13: Order(orderId=27, clientId=400, permId=1779090364, action='SELL', totalQuantity=1.0, orderType='LMT', lmtPrice=9810.0, auxPrice=0.0, tif='DAY', ocaGroup='1779090363', ocaType=3, parentId=26, rule80A='0', openClose='', eTradeOnly=False, firmQuoteOnly=False, volatilityType=0, deltaNeutralOrderType='None', referencePriceType=0, account='DU1340125', clearingIntent='IB', adjustedOrderType='None', cashQty=0.0, dontUseAutoPriceForHedge=True), 14: Order(orderId=28, clientId=400, permId=1779090365, action='SELL', totalQuantity=1.0, orderType='STP', lmtPrice=0.0, auxPrice=9780.0, tif='DAY', ocaGroup='1779090363', ocaType=3, parentId=26, rule80A='0', trailStopPrice=9780.0, openClose='', eTradeOnly=False, firmQuoteOnly=False, volatilityType=0, deltaNeutralOrderType='None', referencePriceType=0, account='DU1340125', clearingIntent='IB', adjustedOrderType='None', cashQty=0.0, dontUseAutoPriceForHedge=True), 15: Order(orderId=19, clientId=400, permId=1779090162, action='BUY', totalQuantity=1.0, orderType='LMT', lmtPrice=9800.0, auxPrice=0.0, tif='DAY', ocaType=3, rule80A='0', openClose='', eTradeOnly=False, firmQuoteOnly=False, volatilityType=0, deltaNeutralOrderType='None', referencePriceType=0, account='DU1340125', clearingIntent='IB', adjustedOrderType='None', cashQty=0.0, dontUseAutoPriceForHedge=True), 16: Order(orderId=20, clientId=400, permId=1779090163, action='SELL', totalQuantity=1.0, orderType='LMT', lmtPrice=9810.0, auxPrice=0.0, tif='DAY', ocaGroup='1779090162', ocaType=3, parentId=19, rule80A='0', openClose='', eTradeOnly=False, firmQuoteOnly=False, volatilityType=0, deltaNeutralOrderType='None', referencePriceType=0, account='DU1340125', clearingIntent='IB', adjustedOrderType='None', cashQty=0.0, dontUseAutoPriceForHedge=True), 17: Order(orderId=21, clientId=400, permId=1779090164, action='SELL', totalQuantity=1.0, orderType='STP', lmtPrice=0.0, auxPrice=9780.0, tif='DAY', ocaGroup='1779090162', ocaType=3, parentId=19, rule80A='0', trailStopPrice=9780.0, openClose='', eTradeOnly=False, firmQuoteOnly=False, volatilityType=0, deltaNeutralOrderType='None', referencePriceType=0, account='DU1340125', clearingIntent='IB', adjustedOrderType='None', cashQty=0.0, dontUseAutoPriceForHedge=True)}, 'orderStatus': {13: OrderStatus(orderId=27, status='PreSubmitted', filled=0.0, remaining=1.0, avgFillPrice=0.0, permId=1779090364, parentId=26, lastFillPrice=0.0, clientId=400, whyHeld='child', mktCapPrice=0.0), 14: OrderStatus(orderId=28, status='PreSubmitted', filled=0.0, remaining=1.0, avgFillPrice=0.0, permId=1779090365, parentId=26, lastFillPrice=0.0, clientId=400, whyHeld='child,trigger', mktCapPrice=0.0), 15: OrderStatus(orderId=19, status='PreSubmitted', filled=0.0, remaining=1.0, avgFillPrice=0.0, permId=1779090162, parentId=0, lastFillPrice=0.0, clientId=400, whyHeld='', mktCapPrice=0.0), 16: OrderStatus(orderId=20, status='PreSubmitted', filled=0.0, remaining=1.0, avgFillPrice=0.0, permId=1779090163, parentId=19, lastFillPrice=0.0, clientId=400, whyHeld='child', mktCapPrice=0.0), 17: OrderStatus(orderId=21, status='PreSubmitted', filled=0.0, remaining=1.0, avgFillPrice=0.0, permId=1779090164, parentId=19, lastFillPrice=0.0, clientId=400, whyHeld='child,trigger', mktCapPrice=0.0)}, 'fills': {13: [], 14: [], 15: [], 16: [], 17: []}, 'log': {13: [TradeLogEntry(time=datetime.datetime(2020, 6, 6, 18, 14, 9, 366578, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='')], 14: [TradeLogEntry(time=datetime.datetime(2020, 6, 6, 18, 14, 9, 366578, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='')], 15: [TradeLogEntry(time=datetime.datetime(2020, 6, 6, 18, 14, 9, 366578, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='')], 16: [TradeLogEntry(time=datetime.datetime(2020, 6, 6, 18, 14, 9, 366578, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='')], 17: [TradeLogEntry(time=datetime.datetime(2020, 6, 6, 18, 14, 9, 374238, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='')]}}

我认为 Stack Overflow 用户可以轻松地将数据复制/粘贴到对象中,然后使用以下代码df_dict将其作为 pandas 数据框返回到其原始状态:df

df = pd.DataFrame.from_dict(df_dict)

但是,当我尝试:

df_dict = <insert the copy/paste data>

它得到一个NameError: name 'Future' is not defined,我不知道如何解决这个问题。关于如何提取数据以允许您使用它,以便您看到我在程序中看到的内容,您有什么想法吗?

继续前进,我的最终目标是让数据看起来像一个带有适当列名的简单 pandas 数据框,例如:

   contract.conId  contract.symbol contract.lastTradeDateOrContractMonth contract.exchange ...
13 357283192       NQ              20200619                              GLOBEX
14 357283192       NQ              20200619                              GLOBEX
.
.
.

导入对象的结构df是:

>>>print (df.dtypes)
contract       object
order          object
orderStatus    object
fills          object
log            object
dtype: object

关于解决这个挑战的任何想法?

标签: pythonpandasinteractive-brokers

解决方案


你可能想用.to_clipboard()这个。

显然,一些更复杂的数据类型,例如Futures对象,将被转换为更通用的数据类型。不过,其他用户不需要导入这些依赖项。


推荐阅读