Why is this pandas data frame not updating with the new prices each time it loops?

670 Views Asked by At

How do I make it so that when I go through this loop, the data frame is updated each time with the new price data?

import time
import pandas as pd
import pycoingecko
cg = pycoingecko.CoinGeckoAPI()

for i in range(10):
    df = pd.DataFrame(cg.get_coin_ticker_by_id(id='bitcoin').get('tickers'))
    time.sleep(2)
1

There are 1 best solutions below

2
On

You just creating another dataframe and dont keep information from the previous loop. You should concatenate dataframes.

import time
import pandas as pd
import pycoingecko
cg = pycoingecko.CoinGeckoAPI()
df= pd.DataFrame()

for i in range(10):
    df = pd.concat([df, pd.DataFrame(cg.get_coin_ticker_by_id(id='bitcoin').get('tickers'))])        
    time.sleep(2)

Creating new dataframe each time:

list = []
for i in range(10):
    df=pd.DataFrame(cg.get_coin_ticker_by_id(id='bitcoin').get('tickers'))
    list.append(df)       
    time.sleep(2)

Here list elements are dataframes.