如何从api响应中获取json并将其展平为panda df

问题描述

在此之前,我在你们的帮助下成功制作了一个交易机器人 -> How to keep the index of my pandas dataframe after normalazation. json

现在我尝试使用 ccxt 库将我的机器人连接到其他交易所。

当我使用 fetch openorder 方法时,我得到一个响应,我认为列表中有一个 json。它通过 ccxt 统一 api 工作。 反正。所以我可以像这样打印列表 [{},{}]。我可以像第一个或第三个订单一样打印,但不是所有没有 [] 的订单。

我希望得到与上面链接的论坛主题完全相同的表格。

这是我尝试过的一些代码和响应。

        openorders = coinbase.fetch_open_orders('BTC/EUR')
        data = openorders[3]
        print('openorders')
        print(openorders)
        print('data')
        print(data)
        pprint(data)
        pprint(openorders)

来自ccxt的输出

[{'id': '7c754fdb-c6dd-44cb-9e99-e780052d6d62','clientOrderId': None,'info': {'id': '7c754fdb-c6dd-44cb-9e99-e780052d6d62','price': '45971.00000000','size': '0.00100000','product_id': 'BTC-EUR','profile_id': '0ef9a216-77ff-469f-a31b-b62393208a5e','side': 'sell','type': 'limit','time_in_force': 'GTC','post_only': False,'created_at': '2021-03-09T20:38:02.495824Z','fill_fees': '0.0000000000000000','filled_size': '0.00000000','executed_value': '0.0000000000000000','status': 'open','settled': False},'timestamp': 1615322282495,'datetime': '2021-03-09T20:38:02.495Z','lastTradeTimestamp': None,'symbol': 'BTC/EUR','timeInForce': 'GTC','postOnly': False,'price': 45971.0,'stopPrice': None,'cost': 0.0,'amount': 0.001,'filled': 0.0,'remaining': 0.001,'fee': {'cost': 0.0,'currency': 'EUR','rate': None},'average': None,'Trades': None},{'id': '9ed743a9-3364-4179-b3e2-936ba6b3d9c7','info': {'id': '9ed743a9-3364-4179-b3e2-936ba6b3d9c7','price': '45997.00000000','created_at': '2021-03-09T20:38:03.507036Z','timestamp': 1615322283507,'datetime': '2021-03-09T20:38:03.507Z','price': 45997.0,{'id': 'abe9d82f-1d5b-4c00-83b7-664cfda76ac8','info': {'id': 'abe9d82f-1d5b-4c00-83b7-664cfda76ac8','price': '46023.00000000','created_at': '2021-03-09T20:38:04.738401Z','timestamp': 1615322284738,'datetime': '2021-03-09T20:38:04.738Z','price': 46023.0,{'id': '69c18563-e862-493e-af89-92984ad4ccdb','info': {'id': '69c18563-e862-493e-af89-92984ad4ccdb','price': '45633.00000000','size': '0.00100600','side': 'buy','created_at': '2021-03-09T20:38:05.492519Z','timestamp': 1615322285492,'datetime': '2021-03-09T20:38:05.492Z','price': 45633.0,'amount': 0.001006,'remaining': 0.001006,{'id': 'abf57e52-22b1-4f67-99ad-ccc0fe64f685','info': {'id': 'abf57e52-22b1-4f67-99ad-ccc0fe64f685','price': '45607.00000000','created_at': '2021-03-09T20:38:06.48702Z','timestamp': 1615322286487,'datetime': '2021-03-09T20:38:06.487Z','price': 45607.0,{'id': '7681b8cf-ebd8-4666-b810-a951c2ea6a93','info': {'id': '7681b8cf-ebd8-4666-b810-a951c2ea6a93','price': '45581.00000000','created_at': '2021-03-09T20:38:07.488203Z','timestamp': 1615322287488,'datetime': '2021-03-09T20:38:07.488Z','price': 45581.0,'Trades': None}]

我想用上面链接中的表格结束。

ps。抱歉不知何故我无法在论坛中得到很好的打印回复

解决方法

您必须将 openorders 从 Python 字典列表转换为 pandas 的 df DataFrame 类型,但是,在此之前,您需要删除所有嵌套的子结构:

# get list of dicts
openorders = coinbase.fetch_open_orders('BTC/EUR')

# remove nested structures
openorders = [coinbase.omit(order,[ 'info','fee','fees' ]) for order in openorders]

# convert to df
df = pd.DataFrame(openorders)

# print the dataframe as table
print(df)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...