Is there a better way of doing this (ideally with a single loop)

Viewed 412

i'm trying to create a pandas dataframe with the Total cryptocurrency marketcap indexed by date. Data are taekn from Coingecko API. I'm able to achieve this with:

import requests
import json

r = requests.get('https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=200')
response = json.loads(r.content)

#suppresses scientific notation
pd.set_option('display.float_format','{:.2f}'.format)

marketcapdf = df(columns=['dates', 'marketcap','volume'])

dates = []
for values in response['prices']:
    dates.append(values[0])

totalmktcap = []
for values in response['market_caps']:
    totalmktcap.append(values[1])

dailymktvolume = []
for values in response['total_volumes']:
    dailymktvolume.append(values[1])

marketcapdf =  df(dates, columns=['Dates'])
marketcapdf = marketcapdf.assign(Total_Marketcap=totalmktcap)
marketcapdf = marketcapdf.assign(Daily_volume=dailymktvolume)

print (marketcapdf)

This results in the correct dataframe:

             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

I was wondering if there's a better way of doing it , pretty sure it is possible to do everything in a single for loop trough list element access. But i need your help to figure out this. If youi want to check the API response, just go https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=200

1 Answers

You can use list comprehensions passed directly to the DataFrame constructor:

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']]})

Or alternatively, using zip in a single comprehension

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]
Related