1

I have a list of csv files I need to open, convert to DataFrames and then save the DataFrames using the same name as the csv file. I can successfully open the csv files but cannot seem to get the DataFrame named correctly.

csv_file_1 = ['X_test_cat']

for f_name in csv_file_1:
    dir_beg = 'C:\\Users\\jsawjsaw1\\Documents\\Python\\'
    end_dir = '.csv'
    f_name = pd.read_csv(dir_beg+f_name+end_dir)

I expect to get a pandas DataFrame named X_test_cat. Instead I get a DataFrame named f_name. FYI - csv_file_1 will eventually have many file names and thus the reason for creating the list. Also, the above code does successfully open the csv file named X_test_cat.csv. I just can't seem to name the DataFrame X_test_cat. Thanks.

John
  • 21
  • 3
  • 1
    Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – glibdud May 10 '19 at 12:33
  • There is an answer on this thread which might help (answer by Jeff): https://stackoverflow.com/questions/38058008/dynamically-rename-data-frame-in-python – Spainey May 10 '19 at 12:34
  • 1
    Yep, that's basically the same advice given to everyone asking about dynamically-named variables: use a dict instead. – glibdud May 10 '19 at 12:36
  • What is the content of `X_test_cat.csv`? What does `print(dir_beg+f_name+end_dir)` show? – Supratim Haldar May 10 '19 at 12:39

1 Answers1

1

Thank you to the few responses I received. I did get some assistance on the issue. below as the resolution.

csv_file_1 = ['X_test_cat']

# Dictionary to hold the dataframes for different csv files
#{'X_test_cat': df_X_test_cat}
dataframes = {}

for f_name in csv_file_1:
    dir_beg = 'C:\\Users\\jsawjsaw1\\Documents\\Python\\'
    end_dir = '.csv'
    df = pd.read_csv(dir_beg+f_name+end_dir, parse_dates=True)    
    df.index.name = 'Index'
    df.set_index(['date', 'ticker'], append=True,inplace=True)    
    dataframes[f_name] = df


X_test_cat = dataframes['X_test_cat']  
John
  • 21
  • 3