create multiple dataframes with python

I have a list filepath with the ubication of the files I need to read (115 files), I need to read those files and add columns header (same for all files), also I have a list with the files names I want to use but no idea in how make it.

filepath look like this

['drive/My Drive/forex/DAT_MT_BCOUSD_M1_2015.csv',
 'drive/My Drive/forex/DAT_MT_BCOUSD_M1_2016.csv',
 'drive/My Drive/forex/DAT_MT_BCOUSD_M1_2017.csv',
 'drive/My Drive/forex/DAT_MT_BCOUSD_M1_2018.csv',
 'drive/My Drive/forex/DAT_MT_BCOUSD_M1_2019.csv']

filename is like this

['DAT_MT_BCOUSD_M1_2015',
 'DAT_MT_BCOUSD_M1_2016',
 'DAT_MT_BCOUSD_M1_2017',
 'DAT_MT_BCOUSD_M1_2018',
 'DAT_MT_BCOUSD_M1_2019']

and I was adding the columns header like this

DAT_MT_BCOUSD_M1_2015= pd.read_csv("drive/My Drive/forex/DAT_MT_BCOUSD_M1_2015.csv",
                                    names=["Date","Time","Bar OPEN Bid Quote","Bar HIGH Bid Quote","Bar 
                                    LOW Bid Quote","Bar CLOSE Bid Quote","Volume"])

any ideas on how aproach the problem?

Upvotes: 0

Views: 37

Answers (1)

Jahziel Rae Arceo
Jahziel Rae Arceo

Reputation: 111

I'd suggest you create a Python dictionary with the dataframes inside, and access them using their filenames as the dictionary keys.

new_dict = {filename: pd.read_csv("drive/My Drive/forex/{}.csv".format(filename),
                                  names=["Date","Time","Bar OPEN Bid Quote",
                                         "Bar HIGH Bid Quote","Bar LOW Bid Quote",
                                         "Bar CLOSE Bid Quote","Volume"]) \
            for filename in filename_list}

Upvotes: 1

Related Questions