0

I added two strings together and now I want to use those strings to create multiple dataframes.... I currently have:

filepath = 'C:/........GC results/lng_11169_fid00'

l = []

for i in range(1,7):
    newpath = filepath + str(i)
    l.append(newpath)

print l 



d =[]

for i in range(1,7):
    dataframes = "df" + str(i)
    d.append(dataframes)
print d
enter code here



m = []
dfstr = "pd.DataFrame.from_csv("

for i in d:
    for x in l:
        newdf =  i + "="+ dfstr + x + ", index_col = None)"
        m.append(newdf)
print m

out:

['df1=pd.DataFrame.from_csv(C:...lng_11169_fid001, index_col = None)', 'df2=pd.DataFrame.from_csv(C:...lng_11169_fid002, index_col = None)'..... + 20 others]

I want to use those strings to make a DataFrame. Is this possible?

Joey
  • 914
  • 4
  • 16
  • 37

2 Answers2

1

An XY problem... Is this what you want? Simply write executable code instead of string concatenation.

import glob
import pandas as pd
path_pattern = 'C:/........GC results/lng_11169_fid00*'
files = glob.glob(path_pattern)
dataframes = [pd.DataFrame.from_csv(f, index_col=None) for f in files]
print dataframes[0]  # yours df1
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
0

This should work

list_ = ['df1=pd.DataFrame.from_csv(C:...lng_11169_fid001, index_col = None)', 'df2=pd.DataFrame.from_csv(C:...lng_11169_fid002, index_col = None)'..... + 20 others]

for code_ in list_:
     exec code_
farhawa
  • 10,120
  • 16
  • 49
  • 91