-1

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)
Andrea Di Iura
  • 467
  • 5
  • 11
Greg
  • 1
  • 1
  • 2
    You are just creating a new dataframe at each iteration, what you should do is to append a row to the existing dataframe. – yeyosef Mar 15 '22 at 21:06
  • What do you mean? I want to use the entire data frame but the data is not updating every couple of seconds – Greg Mar 15 '22 at 21:10
  • Does coingecko actually update the data every two seconds? – ForceBru Mar 15 '22 at 21:11
  • I have tried it with 10 seconds as well. The volume data is not changing in the data frame either. – Greg Mar 15 '22 at 21:13

1 Answers1

0

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.

yeyosef
  • 78
  • 9
  • While this works, it is adding to the current data frame. I want it to make a new data frame each time. Do you know how I would go about doing this? – Greg Mar 15 '22 at 21:17
  • You can just append dataframes to a list, answer edited. – yeyosef Mar 15 '22 at 21:28