How to add a column inside a for loop in Pandas without it breaking

48 Views Asked by At

I have the following code:

from polygon import RESTClient
key = ''
all_tickers = [
    # {'ticker': 'A', 'name': 'Agilent Technologies Inc.', 'market': 'stocks', 'locale': 'us', 'primary_exchange': 'XNYS', 'type': 'CS', 'active': True, 'currency_name': 'usd', 'cik': '0001090872', 'composite_figi': 'BBG000C2V3D6', 'share_class_figi': 'BBG001SCTQY4', 'last_updated_utc': '2022-03-16T00:00:00Z'}, {'ticker': 'AA', 'name': 'Alcoa Corporation', 'market': 'stocks', 'locale': 'us', 'primary_exchange': 'XNYS', 'type': 'CS', 'active': True, 'currency_name': 'usd', 'cik': '0001675149', 'composite_figi': 'BBG00B3T3HD3', 'share_class_figi': 'BBG00B3T3HF1', 'last_updated_utc': '2022-03-16T00:00:00Z'}
    {
    "ticker": "ATNI",
    "name": "ATN International, Inc",
    "market": "stocks",
    "locale": "us",
    "primary_exchange": "XNAS",
    "type": "CS",
    "active": True,
    "currency_name": "usd",
    "cik": "0000879585",
    "composite_figi": "BBG000F8RCH1",
    "share_class_figi": "BBG001S9SMF2",
    "last_updated_utc": "2022-03-27T00:00:00Z"
  }, {
    "ticker": "ATNM",
    "name": "Actinium Pharmaceuticals, Inc",
    "market": "stocks",
    "locale": "us",
    "primary_exchange": "XASE",
    "type": "CS",
    "active": True,
    "currency_name": "usd",
    "cik": "0001388320",
    "composite_figi": "BBG000RQZ245",
    "share_class_figi": "BBG001SV3L17",
    "last_updated_utc": "2022-03-27T00:00:00Z"
  }
    ]

I then try to loop through the tickers and append them to a dataframe while adding the ticker as a column to each of the lists:

with RESTClient(key) as client:
    from_ = "2020-01-09"
    to = "2021-01-10"
    df_list = []
    
    for i in all_tickers:
        ticker =  i['ticker']
        r = client.stocks_equities_aggregates(ticker, 1, "day", from_, to, unadjusted=False)
    
        print(f"Daily aggregates for {r.ticker} between {from_} and {to}.")

        try:
            df = pd.DataFrame(r.results)
            df['ticker'] = ticker
            df_list.append(df)

        except:
            print('nothing')
    combined_df = pd.concat(df_list,ignore_index=True)
    print(combined_df, 'comb')

But here's the result I get:

Daily aggregates for ATNI between 2020-01-09 and 2021-01-10.
Daily aggregates for ATNM between 2020-01-09 and 2021-01-10.
            v       vw      o      c        h      l              t     n  \
0     58871.0  57.2358  57.22  56.99  57.7600  56.81  1578546000000  1364   
1     72964.0  56.3568  57.10  55.69  57.2400  55.59  1578632400000  1499   
2     33738.0  55.9086  55.70  56.10  56.2000  55.45  1578891600000   950   
3     98525.0  55.8561  55.86  55.97  56.2400  55.50  1578978000000  1777   
4     31869.0  56.0751  55.89  55.85  56.4400  55.69  1579064400000   940   
..        ...      ...    ...    ...      ...    ...            ...   ...   
501  895346.0   8.0596   7.99   7.86   8.3560   7.78  1609736400000  4429   
502  642829.0   8.2878   8.05   8.57   8.6000   7.89  1609822800000  3249   
503  666115.0   8.1616   8.55   8.02   8.5800   7.95  1609909200000  3778   
504  700868.0   8.2889   8.03   8.58   8.5900   8.02  1609995600000  2973   
505  376746.0   8.3241   8.42   8.24   8.5511   8.12  1610082000000  3052   

    ticker  
0     ATNI  
1     ATNI  
2     ATNI  
3     ATNI  
4     ATNI  
..     ...  
501   ATNM  
502   ATNM  
503   ATNM  
504   ATNM  
505   ATNM  

[506 rows x 9 columns] comb

If I remove the 'df['ticker'] = ticker' line it works as expected, giving me both lists joined together, like so:

Daily aggregates for ATNI between 2020-01-09 and 2021-01-10.
Daily aggregates for ATNM between 2020-01-09 and 2021-01-10.
            v       vw      o      c        h      l              t     n
0     58871.0  57.2358  57.22  56.99  57.7600  56.81  1578546000000  1364
1     72964.0  56.3568  57.10  55.69  57.2400  55.59  1578632400000  1499
2     33738.0  55.9086  55.70  56.10  56.2000  55.45  1578891600000   950
3     98525.0  55.8561  55.86  55.97  56.2400  55.50  1578978000000  1777
4     31869.0  56.0751  55.89  55.85  56.4400  55.69  1579064400000   940
..        ...      ...    ...    ...      ...    ...            ...   ...
501  895346.0   8.0596   7.99   7.86   8.3560   7.78  1609736400000  4429
502  642829.0   8.2878   8.05   8.57   8.6000   7.89  1609822800000  3249
503  666115.0   8.1616   8.55   8.02   8.5800   7.95  1609909200000  3778
504  700868.0   8.2889   8.03   8.58   8.5900   8.02  1609995600000  2973
505  376746.0   8.3241   8.42   8.24   8.5511   8.12  1610082000000  3052

How do I join both the lists together and include the ticker symbol as a column, without breaking the dataframe?

How I need the DF to look:

            v       vw      o      c        h      l              t  n     ticker 
0     58871.0  57.2358  57.22  56.99  57.7600  56.81  1578546000000  4424  ATNI
...
250  895346.0   8.0596   7.99   7.86   8.3560   7.78  1609736400000  4429  ATNM
...
0

There are 0 best solutions below