简体   繁体   English

从包含带有嵌套字典的元组的python列表创建一个pandas数据框

[英]create a pandas dataframe from python list containing tuple with nested dictionary

I have wrestled with this for a few days now, but can't figure it out.我已经为此挣扎了几天,但无法弄清楚。 I'm trying to create a dataframe "account_activity" from the results of an api get.我正在尝试根据 api get 的结果创建一个数据框“account_activity”。 i make an api call and print it out.我进行了 api 调用并将其打印出来。

account_activities = api.get_activities()
print(account_activities)

returns:返回:

[AccountActivity({   'activity_type': 'FILL',
    'cum_qty': '100',
    'id': '20211111105648607::a0ef3f04-ff00-4b8e-834d-54737d89c332',
    'leaves_qty': '0',
    'order_id': '32c9a40e-e6d2-4c7c-8949-a39ad32b535f',
    'order_status': 'filled',
    'price': '187.09',
    'qty': '56',
    'side': 'sell',
    'symbol': 'U',
    'transaction_time': '2021-11-11T15:56:48.607222Z',
    'type': 'fill'})]

How do I create a dataframe "account_activity" where the keys are the column headers and the index is the transaction_time is the row index with values in the rows?如何创建数据框“account_activity”,其中键是列标题,索引是 transaction_time 是行索引,行中有值?

Assuming j is te JSON from your AccountActivity object:假设j是来自您的AccountActivity对象的 JSON:

df = pd.DataFrame(j, index=['']).set_index('transaction_time',drop=True)

How you get the JSON depends on the APIs you're using.获取 JSON 的方式取决于您使用的 API。 Perhaps也许

j = account_activities[0].__dict__

will work?将工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM