簡體   English   中英

如何在python中將api.Response轉換為數據框或json?

[英]How to convert api.Response to data frame or json in python?

我有這樣的數據,我想將其轉換為數據框。

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': 15829.5209.520730000000000000000000 , '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, '報價':{ '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')

塊引用

類型錯誤:“響應”對象不可迭代

2)

crypto_map = pd.DataFrame(t)

塊引用

ValueError:未正確調用 DataFrame 構造函數!

3)

t.json()

塊引用

值錯誤:未定義名稱“加載”

4)我想在數據中加載一個變量,但它給了我這個錯誤:

t['active_exchanges']

塊引用

類型錯誤:“響應”對象不可下標

這兩天一直在為這個問題苦苦掙扎,找不到解決的方法,請幫忙。 謝謝

使用t.data提取您的數據響應

文檔

如果調用成功,所有端點都以 JSON 格式返回數據,並在 data 下返回查詢結果。

因此,您可以像這樣創建數據框:

df = pd.DataFrame(t.data)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM