0

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

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:“响应”对象不可迭代

  1. crypto_map = pd.DataFrame(t)

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

t.json()

ValueError:未定义名称“加载”

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

t['active_exchanges']

TypeError:“响应”对象不可下标

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

4

1 回答 1

1

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

文档

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

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

df = pd.DataFrame(t.data)
于 2021-11-13T12:15:07.840 回答