0

The following dataframe etf_list is given:

etf_list = pd.DataFrame({'ISIN': ['IE00B4X9L533', 'IE00B0M62Q58', 'LU0292097234', 'IE00BF4RFH31'],
        'Name': ['HSBC MSCI WORLD UCITS ETF', 'iShares MSCI World UCITS ETF', 'FTSE 100 Income UCITS ETF 1D', 'iShares MSCI World Small Cap UCITS ETF'],
        'Anbieter': ['HSBC', 'iShares', 'Xtrackers', 'iShares' ],
        'Extension': ['xls', 'csv', 'xlsx', 'csv' ]})

In the folder /ETF I have the following files, which were generated today on June 11, 2021:

  • IE00B4X9L533_20210611.xls
  • IE00B0M62Q58_20210611.csv
  • LU0292097234_20210611.xlsx
  • IE00BF4RFH31_20210611.csv

As you can see, the files have the following structure:

etf_list['ISIN'] + '_' + timestr + '.' + etf_list['Extension']

whereas timestr = time.strftime("%Y%m%d")

The objective is to create in a for loop dataframes for the files, where Anbieter in etf_list equals 'iShares'. The created dataframes shall have the name of ISIN in the dataframe etf_list. In order to achieve this I defined an empty dictionary df ={}

df = {} 
for i, row in etf_list.iterrows():
if row['Anbieter']=='iShares':        
    df[row['ISIN']] = 'ETF/'+ row['ISIN'] + '_' + timestr + '.csv'        
    df[row['ISIN']] = pd.read_csv(df[row['ISIN']], sep=',',skiprows=2, thousands='.', decimal=',')
else:
    pass

The problem with this approach is, in order to reference to the created dataframes, I have to call them with for instance df['IE00B0M62Q58'] or df['IE00BF4RFH31'], but my objective is to use IE00B0M62Q58 instead df['IE00B0M62Q58'] and IE00BF4RFH31 instead of df['IE00BF4RFH31'].

What do I have to do in order reach my goal? How do I have to adjust my code?

  • 1
    Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – Henry Ecker Jun 11 '21 at 17:19
  • It sounds like you're looking for something like https://stackoverflow.com/a/53503041/15497888 or https://stackoverflow.com/a/1373201/15497888 – Henry Ecker Jun 11 '21 at 17:20
  • Ty, I had this suggestion recently, but unfortunately, it doesn't answer my question. – Economist Learning Python Jun 11 '21 at 17:21
  • Use ``eval`` so you can code a string as a variable name. https://www.programiz.com/python-programming/methods/built-in/eval – xyzjayne Jun 11 '21 at 17:37
  • But in general I wouldn't recommend that. ``eval`` statements can be messy to maintain. Are you sure you can't change downstream code to avoid this? – xyzjayne Jun 11 '21 at 17:38
  • @xyzjayne Ty, honestly, I'm not sure, I'm actually not a programmer and I find it very difficult to understand the answers. Would it be possible for you to adjust my for loop code according to your suggestion and I will check if it works. That would be of great help to me. – Economist Learning Python Jun 11 '21 at 17:43
  • @HenryEcker Hi, would it be possible for you to adjust the for loop according to your proposed solution to check whether it works? I'm kind of stuck and can't get any further, that would help me a lot, thank you! – Economist Learning Python Jun 11 '21 at 18:55

1 Answers1

0

You can exec to use strings as variable names.

for i, row in etf_list.iterrows():
if row['Anbieter']=='iShares':    
    ISIN = row['ISIN']   
    file_name = 'ETF/'+ ISIN + '_' + timestr + '.csv'
    command_str = f"{ISIN} = pd.read_csv('{file_name}', sep=',',skiprows=2, thousands='.', decimal=',')" # f doesn't work in Python 2, use format or % instead
    exec(command_str)
    
else:
    pass
xyzjayne
  • 1,331
  • 9
  • 25
  • Ty, unfortunately I get an error: File "", line 5 command_str = f"{ISIN} = pd.read_csv({file_name}, sep=',',skiprows=2, thousands='.', decimal=',') ^ SyntaxError: EOL while scanning string literal – Economist Learning Python Jun 11 '21 at 18:10
  • Sorry, just edited. Make sure you keep the double quote at the end of the line? – xyzjayne Jun 11 '21 at 18:11
  • Ty, I did, unfortunately still getting an error: File "", line 1 IE00B0M62Q58 = pd.read_csv(ETF/IE00B0M62Q58_20210611.csv, sep=',',skiprows=2, thousands='.', decimal=',') ^ SyntaxError: invalid syntax – Economist Learning Python Jun 11 '21 at 18:15
  • Just fixed it - try again? – xyzjayne Jun 11 '21 at 18:17
  • Ty, but still getting an error: File "", line 1 IE00B0M62Q58 = pd.read_csv('ETF/IE00B0M62Q58_20210611.csv', sep=',',skiprows=2, thousands='.', decimal=',') ^ SyntaxError: invalid syntax PyCharm shows an error where IE00B0M62Q58 equals pd.read_csv(... – Economist Learning Python Jun 11 '21 at 18:22
  • My bad, the correct term is exec not eval. – xyzjayne Jun 11 '21 at 18:57
  • 1
    Thank youuuuu soooo much! Works perfectly fine. That helps me a lot! I was already exhausted, but you saved my life! Ty! A little suggestion may be u should replace eval with exec in your first line in the answer, but again ty very much! – Economist Learning Python Jun 11 '21 at 19:03