【问题标题】:how to get a json from api response and flatten it to panda df如何从 api 响应中获取 json 并将其展平为 panda df
【发布时间】:2021-03-09 20:14:50
【问题描述】:

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

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

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

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

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

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

我想最终得到一个类似上面链接中的表格。

ps。抱歉,我无法在论坛中得到很好的回复?

【问题讨论】:

  • “我得到一个响应,我认为列表中有一个 json”。关于术语的澄清:没有“json”之类的东西。在 JSON 格式中,{} 表示我们所说的 object。在 Python 中,等效的数据结构是 dictionary。对于 ccxt 库,我怀疑它发出 HTTP 请求并接收回 JSON 字符串。当您在代码中看到此数据时,它已被解析为 Python 字典……或者更准确地说是字典列表。有了这个术语,您可以在 Google 上搜索“数据框的字典列表”之类的内容来获得帮助。
  • 这会导致这样的链接^

标签: python json api response ccxt


【解决方案1】:

您必须将 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)

【讨论】:

  • 哇,给你很多赞。我的问题太糟糕了,但你的回答太完美了。
猜你喜欢
  • 2021-08-19
  • 2022-11-25
  • 1970-01-01
  • 2019-04-25
  • 2018-05-20
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
相关资源
最近更新 更多