The goal here is to flatten data.json()
which is a dict
returned by requests
.
Flattening dictionaries is a reasonable complex topic because of the various types of values that can be found within. But here, the data feed is known.
Therefore, a simply flattening function can be found from various python recipes and functionally applied to the data obtained from the data feed.
import json
import itertools
import requests
import json
import pandas as pd
def get_cryptocompare_data():
url = "https://min-api.cryptocompare.com/data/top/mktcapfull?limit=15&tsym=USD"
response = requests.get(url)
d = response.json()
return d
# https://codereview.stackexchange.com/questions/21033/flatten-dictionary-in-python-functional-style
def flatten_dict(d):
def items():
for key, value in d.items():
if isinstance(value, dict):
for subkey, subvalue in flatten_dict(value).items():
yield key + "." + subkey, subvalue
else:
yield key, value
return dict(items())
d = get_cryptocompare_data()
data = d['Data']
data = list(map(flatten_dict, data))
df = pd.DataFrame(data)
print(df[['CoinInfo.Name', 'CoinInfo.Type', 'RAW.USD.TYPE', 'RAW.USD.SUPPLY', 'DISPLAY.USD.SUPPLY']])
Will provide the following name, type and supply columns
CoinInfo.Name CoinInfo.Type RAW.USD.TYPE RAW.USD.SUPPLY DISPLAY.USD.SUPPLY
0 BTC 1 5 1.764898e+07 Ƀ 17,648,975.0
1 XRP 1 5 9.999185e+10 XRP 99,991,850,794.0
2 ETH 1 5 1.056861e+08 Ξ 105,686,063.1
3 EOS 1 5 1.041886e+09 EOS 1,041,886,454.0
4 BCH 1 5 1.764895e+07 BCH 17,648,946.0
5 LTC 1 5 6.136893e+07 Ł 61,368,933.6
6 BNB 1 5 1.883456e+08 BNB 188,345,602.2
7 USDT 1 5 2.468906e+09 ₮ 2,468,905,774.0
8 XLM 1 5 1.932851e+10 XLM 19,328,512,453.0
9 ADA 1 5 2.592707e+10 ADA 25,927,070,538.0