Вы можете использовать list comprehensions
, переданный непосредственно конструктору DataFrame
:
import requests
import json
import pandas as pd
r = requests.get('https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=200')
response = json.loads(r.content)
marketcapdf = pd.DataFrame({
'Dates': [x[0] for x in response['prices']],
'Total_Marketcap': [x[1] for x in response['market_caps']],
'Daily_volume': [x[1] for x in response['total_volumes']]})
Или, альтернативно, использовать zip
в единственное понимание
marketcapdf = pd.DataFrame([(p[0], m[1], v[1])
for p, m, v in zip(*response.values())],
columns=['Dates', 'Total_Marketcap', 'Daily_volume'])
marketcapdf
[out]
Dates Total_Marketcap Daily_volume
0 1566518400000 181311699889.67 20144228964.47
1 1566604800000 187878412535.86 18313019852.75
2 1566691200000 181764456091.20 21187546608.00
3 1566777600000 180491202154.33 17899166778.01
4 1566864000000 184989701185.55 23392461462.61
.. ... ... ...
196 1583452800000 165070261612.79 38122635173.15
197 1583539200000 166791067805.43 41388087588.44
198 1583625600000 162543409238.00 36764763103.12
199 1583712000000 147076617271.69 39132209137.70
200 1583767255000 142178026150.74 50332115525.77
[201 rows x 3 columns]