Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch data from API n times and then convert it into a single pandas dataframe

Tags:

python

pandas

I have this small python script in which i'm fetching cryptocurrency exchange rate data every second.Now I want to stop after fetching data lets say 100 times and then convert all those data into a single dataframe. Also is scheduler the right way to do this?If no then what else should I be using?

import requests
import json
import pandas as pd
import sched, time

s = sched.scheduler(time.time, time.sleep)
def my_function(sc): 
    data = requests.get("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR").json()
    print(data)
    s.enter(1, 1, my_function, (sc,))
s.enter(1, 1, my_function, (s,))
s.run()
like image 922
klaptor Avatar asked Nov 25 '25 08:11

klaptor


1 Answers

Maybe something like this:

import requests
import json
import pandas as pd
import time
from matplotlib import pyplot as plt
from pandas import DataFrame

eur_collection = []
usd_collection = []
btc_collection = []

for i in range(100):
    print("Request {}".format(i))
    data = requests.get("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR").json()
    eur_collection.append(data["EUR"])
    usd_collection.append(data["USD"])
    btc_collection.append(data["BTC"])
    time.sleep(0.01)

dframe = DataFrame({
    'eur': eur_collection, 
    'usd': usd_collection, 
    'btc': btc_collection
})

dframe.plot()

plt.show()
like image 147
andreihondrari Avatar answered Nov 26 '25 22:11

andreihondrari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!