0

I have a set of csv files in a folder, and I would like to create dynamically dataframes having the same name as the csv file

for instance I have the following files : AC201703.csv AC201703.csv AC201704.csv I would like to load those file an put in dataframes having the same name e.g. AC201703 AC201704 I try to use exec like the following

for file in glob.glob("*.csv"):
   df = pd.read_csv(file,encoding = 'ISO-8859-1' )
   exec("%s = %s" % (file[:7],df)) 

but it didn't work, i get the following error

  File "<ipython-input-31-2d670638b427>", line 4, in <module>
    exec("%s = %s" % (file[:7],df))

  File "<string>", line 1
    A201610 =        A201610_fb_act_n_n_buy cust_key  A201610_fb_act_n_n LCELGST  \
                                                   ^
SyntaxError: invalid syntax

it seems to assign to the variable A201610 the name of csv columns

raton roguille
  • 181
  • 2
  • 11

1 Answers1

3

I think better is use dict of DataFrames:

d = {file[:7] : pd.read_csv(file, encoding = 'ISO-8859-1') for file in glob.glob("*.csv")}

and then select DataFrame by keys - first 7 letters of filenames:

print (d['AC20170'])

But if dont want use dicts:

for file in glob.glob("*.csv"):
    globals()[file[:7]] = pd.read_csv(file,encoding = 'ISO-8859-1')

print (AC20170)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • can you help https://stackoverflow.com/questions/47305897/matching-rows-between-dataframes-in-pandas-in-python/47307029#47307029 – Pyd Nov 15 '17 at 12:35
  • @pyd - really hard, there is necessary some fuzzy matching and I have no idea for nice fast solution :( I try find some `difflib` solutions like `df2['values'].dropna().apply(lambda x: difflib.get_close_matches(x, df1['Names']))` but it failed :( – jezrael Nov 15 '17 at 12:36