简体   繁体   中英

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. i make an api call and print it out.

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?

Assuming j is te JSON from your AccountActivity object:

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

How you get the JSON depends on the APIs you're using. Perhaps

j = account_activities[0].__dict__

will work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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