I have a list of dataframe objects:
mydfs= [df1,df2,df3]
And a list of names I'd like to assign to them:
mynames = ['customers','transactions','dates']
I can't figure out how to get them out of the list. I just want to index these dataframes directly like customers['name']
.
I've tried just assigning them directly using zip:
for name,df in zip(mynames, mydfs):
name = df
But when I check customers.head()
etc. it is not defined.
How can I get them out of this list? I want them out of the list and as regular dataframes that I can start indexing as normal.
Solution: when reading them in use globals() to assign a name to the DF. Skip adding them to the list entirely.
for filename in all_files:
name = ntpath.basename(filename)
name = name.replace('.dat','')
try:
globals()[name] = pd.read_csv(filename, sep='\t', engine='python')
except:
print(f'File not read:{filename}')