1

I have many dataframes like the following:

weight=pd.read_csv('weight.csv',index_col=0)
inventory=pd.read_csv('inventory1.csv',index_col=0)

which are to be stored in an array:

df_array=[weight,inventory,etc]

to be passed as arguments subsequently, to another function. When I run this:

df_array[0]

I get:

Out[15]: 
     C11   C12   C13   C21   C22   C23   C31  C32   C33   
C1  1.00  1.00  1.00  1.50  2.00  2.50  2.50  3.0  3.50     
C2  0.40  0.50  0.67  1.00  1.00  1.00  1.50  2.0  2.50    
C3  0.28  0.33  0.40  0.40  0.50  0.67  1.00  1.0  1.00   

These are the rows and columns of the dataframe weight. What I want is for df_array[0] to print the name of the dataframe (weight) as a variable name without quotes which I can pass as argument to another function. Is it possible? Or is there any alternative for it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
IndigoChild
  • 842
  • 3
  • 11
  • 29

1 Answers1

1

I think the best is use dictionary of DataFrames instead list of DataFrames:

df_dict={'weight': weight,'inventory': inventory}

and then select by keys:

df1 = df_dict['weight']
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • hi, when I try this:df_dict.keys()[0], I get this error:TypeError: 'dict_keys' object does not support indexing – IndigoChild Feb 24 '18 at 11:53
  • @DChy that's right, dictionary keys aren't ordered so asking for the "first" doesn't make sense. – jonrsharpe Feb 24 '18 at 11:56
  • @DChy - hmmm, dictionary are [not ordered](https://stackoverflow.com/q/22333388/2901002). I only suggest better way for store Dataframes. – jezrael Feb 24 '18 at 11:56