将包含多个对象的熊猫对象转换为单个熊猫数据框的过程是什么?

时间:2020-06-06 19:22:23

标签: python pandas interactive-brokers

使用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中,然后使用以下代码将其作为熊猫数据框df返回到其原始状态:< / p>

df = pd.DataFrame.from_dict(df_dict)

但是,当我尝试时:

df_dict = <insert the copy/paste data>

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

继续,我的最终目标是使数据看起来像带有适当列名的简单熊猫数据框,例如:

   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

对解决这一挑战有何想法?

1 个答案:

答案 0 :(得分:0)

您可能希望为此使用.to_clipboard()

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