'Why is this pandas data frame not updating with the new prices each time it loops?
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)
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
