Python 将包含多个对象的pandas对象转换为单个pandas数据帧的过程是什么
使用交互式代理API,有时需要将我的数据转换为简单的二维数据帧。下面是从API返回的交互式代理数据示例。为了允许堆栈溢出用户查看实际数据,我尝试将其置于易于检索的形式:Python 将包含多个对象的pandas对象转换为单个pandas数据帧的过程是什么,python,pandas,interactive-brokers,Python,Pandas,Interactive Brokers,使用交互式代理API,有时需要将我的数据转换为简单的二维数据帧。下面是从API返回的交互式代理数据示例。为了允许堆栈溢出用户查看实际数据,我尝试将其置于易于检索的形式: 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', lastTrade
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='')]}}
我认为堆栈溢出用户可以很容易地将数据复制/粘贴到对象df_dict
,然后使用以下代码将其作为数据帧df
返回到原始状态:
df = pd.DataFrame.from_dict(df_dict)
但是,当我尝试时:
df_dict = <insert the copy/paste data>
导入对象的结构,df
为:
>>>print (df.dtypes)
contract object
order object
orderStatus object
fills object
log object
dtype: object
关于解决这一挑战,你有什么想法吗?你可能想用它来解决这个问题
显然,一些更复杂的数据类型,如Futures
对象,将转换为更通用的数据类型。但是,其他用户不需要导入这些依赖项。您可能希望为此使用
显然,一些更复杂的数据类型,如
Futures
对象,将转换为更通用的数据类型。其他用户不需要导入这些依赖项。我想Future(…)
是类Future
的构造函数。导入类Future
它应该可以正常工作。大多数导入的数据结构取决于Future对象(每个合同号都是Future对象)。您必须导入或定义我猜Future(…)
是类Future
的构造函数。导入类Future
它应该可以正常工作。大多数导入的数据结构取决于Future对象(每个合同号都是Future对象)。你必须导入或定义我不明白这有什么帮助。我按照您的建议,使用.to_clipboard()
将其移动到剪贴板。如果我要将它从剪贴板粘贴到堆栈溢出问题中,我或其他人会使用什么python代码将其恢复到程序中的表单中?使用.to_clipboard()时,将其转换为.csv,然后使用.read_clipboard()再次返回。当它返回时,似乎会更改文件结构。我不确定这对解决我的问题是否有帮助。我不明白这有什么帮助。我按照您的建议,使用.to_clipboard()
将其移动到剪贴板。如果我要将它从剪贴板粘贴到堆栈溢出问题中,我或其他人会使用什么python代码将其恢复到程序中的表单中?使用.to_clipboard()时,将其转换为.csv,然后使用.read_clipboard()再次返回。当它返回时,似乎会更改文件结构。我不确定这对解决我的问题是否有帮助。
>>>print (df.dtypes)
contract object
order object
orderStatus object
fills object
log object
dtype: object