简体   繁体   中英

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

I have a data like this and I want to convert it to data frame.

t = cmc.globalmetrics_quotes_latest()

(Cmc is 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'}"""

I tried :

1)

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

Blockquote

TypeError: 'Response' object is not iterable

2)

crypto_map = pd.DataFrame(t)

Blockquote

ValueError: DataFrame constructor not properly called!

3)

t.json()

Blockquote

value Error : name 'load' is not defined

4) I want to load a variable in a data but it gives me this error:

t['active_exchanges']

Blockquote

TypeError: 'Response' object is not subscriptable

I been struggling with this for the last couple of days and couldn't find a way to solve it, please help. Thanks

Use t.data to extract your data response

From the documentation :

All endpoints return data in JSON format with the results of your query under data if the call is successful.

So, you can create your dataframe like this:

df = pd.DataFrame(t.data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM