Home > OS >  Using concat functions instead of append
Using concat functions instead of append

Time:03-21

I am trying to use concat function instead of append to produce the same output from this block of code:

final_dataframe = pd.DataFrame(columns = my_columns)

for symbol in stocks['Ticker'][:5]:
    api_url = f'https://sandbox.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_TOKEN}'
    data = requests.get(api_url).json()
    final_dataframe = final_dataframe.append(
                                    pd.Series([symbol, 
                                               data['latestPrice'], 
                                               data['marketCap'], 
                                               'N/A'], 
                                              index = my_columns), 
                                    ignore_index = True)

The output is this:

    Ticker  Stock_price Market_capitalization   Number_of_shares_to_buy
0   A         144.3       42532431075                   N/A
1   AAL       17.16       11068908461                   N/A
2   AAP       210.74      12880265047                   N/A
3   AAPL      167.43      2762301276735                 N/A
4   ABBV      164.5       287271211810                  N/A

CodePudding user response:

This is not an optimal strategy to append data row by row to a DataFrame. First collect your data into a Python data structure (here a list of dict) then create a dataframe from this data structure.

Try:

tickers = []
for symbol in stocks['Ticker'][:5]:
    api_url = f'https://sandbox.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_TOKEN}'
    data = requests.get(api_url).json()
    d = dict(zip(my_columns, [symbol, data['latestPrice'], data['marketCap'], 'N/A']))
    tickers.append(d)
df = pd.DataFrame(tickers)

Output:

>>> df
  Ticker  Stock_price  Market_capitalization  Number_of_shares_to_buy
0      A       144.30            42532431075                      NaN
1    AAL        17.16            11068908461                      NaN
2    AAP       210.74            12880265047                      NaN
3   AAPL       167.43          2762301276735                      NaN
4   ABBV       164.50           287271211810                      NaN
  • Related