【问题标题】:How to convert api.Response to data frame or json in python?如何将 api.Response 转换为 python 中的数据框或 json?
【发布时间】:2021-12-25 10:58:45
【问题描述】:

我有这样的数据,我想将其转换为数据框。

t = cmc.globalmetrics_quotes_latest()

(Cmc 是 coinmarketcap api)

type(t)=
coinmarketcapapi.response
"""
RESPONSE: 820ms OK: {'active_cryptocurrencies': 7336, 'total_cryptocurrencies': 14027, 'active_market_pairs': 48398, 'active_exchanges': 431, 'total_exchanges': 1527, 'eth_dominance': 19.65826035511, 'btc_dominance': 43.062294678908, 'eth_dominance_yesterday': 19.3097174, 'btc_dominance_yesterday': 43.49544924, 'eth_dominance_24h_percentage_change': 0.34854295511, 'btc_dominance_24h_percentage_change': -0.433154561092, 'defi_volume_24h': 27083851925.97366, 'defi_volume_24h_reported': 27083851925.97366, 'defi_market_cap': 170702982573.4028, 'defi_24h_percentage_change': 10.226566098235, 'stablecoin_volume_24h': 135386869618.86761, 'stablecoin_volume_24h_reported': 135386869618.86761, 'stablecoin_market_cap': 136340827788.9902, 'stablecoin_24h_percentage_change': 25.498668079553, 'derivatives_volume_24h': 292224255894.04266, 'derivatives_volume_24h_reported': 292224255894.04266, 'derivatives_24h_percentage_change': 34.750223263748, 'quote': {'USD': {'total_market_cap': 2837427293667.6865, 'total_volume_24h': 172324325231.62, 'total_volume_24h_reported': 172324325231.62, 'altcoin_volume_24h': 125164009565.61545, 'altcoin_volume_24h_reported': 125164009565.61545, 'altcoin_market_cap': 1615565991168.745, 'defi_volume_24h': 27083851925.97366, 'defi_volume_24h_reported': 27083851925.97366, 'defi_24h_percentage_change': 10.226566098235, 'defi_market_cap': 170702982573.4028, 'stablecoin_volume_24h': 135386869618.86761, 'stablecoin_volume_24h_reported': 135386869618.86761, 'stablecoin_24h_percentage_change': 25.498668079553, 'stablecoin_market_cap': 136340827788.9902, 'derivatives_volume_24h': 292224255894.04266, 'derivatives_volume_24h_reported': 292224255894.04266, 'derivatives_24h_percentage_change': 34.750223263748, 'last_updated': '2021-11-11T15:57:10.999Z', 'total_market_cap_yesterday': 2968337016970.539, 'total_volume_24h_yesterday': 141372403925.96, 'total_market_cap_yesterday_percentage_change': -4.410204183501307, 'total_volume_24h_yesterday_percentage_change': 21.89389190967583}}, 'last_updated': '2021-11-11T15:57:10.999Z'}"""

我试过了:

1)

w=pd.DataFrame.from_dict(pd.json_normalize(t), orient='columns')

TypeError: 'Response' 对象不可迭代

  1. crypto_map = pd.DataFrame(t)

ValueError: DataFrame 构造函数未正确调用!

t.json()

ValueError: name 'load' 没有定义

我想在数据中加载一个变量,但它给了我这个错误:

t['active_exchanges']

TypeError: 'Response' 对象不可下标

过去几天我一直在为此苦苦挣扎,但找不到解决方法。

【问题讨论】:

  • 你试过t.data吗?
  • 成功了,谢谢!

标签: python json pandas api coinmarketcap


【解决方案1】:

使用t.data 提取您的数据响应

来自documentation

如果调用成功,所有端点都会以 JSON 格式返回数据,并在 data 下显示您的查询结果。

因此,您可以像这样创建数据框:

df = pd.DataFrame(t.data)

【讨论】:

猜你喜欢
  • 2017-05-25
  • 2017-10-31
  • 1970-01-01
  • 2020-05-29
  • 2021-06-20
  • 2017-05-01
  • 2021-12-04
  • 2018-11-20
  • 1970-01-01
相关资源
最近更新 更多