1

I need to give names to previously defined dataframes.

I have a list of dataframes :

liste_verif = ( dffreesurfer,total,qcschizo)

And I would like to give them a name by doing something like:

for h in liste_verif: 
    h.name = str(h)

Would that be possible ?

When I'm testing this code, it's doesn't work : instead of considering h as a dataframe, python consider each column of my dataframe. I would like the name of my dataframe to be 'dffreesurfer', 'total' etc...

salim
  • 321
  • 1
  • 2
  • 12
  • You should consider just storing your dfs in a dict if you want to map a string name to a df – EdChum Oct 25 '16 at 09:24

1 Answers1

0

You can use dict comprehension and map DataFrames by values in list L:

dffreesurfer = pd.DataFrame({'col1': [7,8]})
total = pd.DataFrame({'col2': [1,5]})
qcschizo = pd.DataFrame({'col2': [8,9]})

liste_verif = (dffreesurfer,total,qcschizo)
L = ['dffreesurfer','total','qcschizo']

dfs = {L[i]:x for i,x in enumerate(liste_verif)}
print (dfs['dffreesurfer'])
   col1
0     7
1     8

print (dfs['total'])
   col2
0     1
1     5
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252