1

I have a dictionary as dict_q as:

{'a': w_1 w_2 w_3 w_4
      3    3   3   3 
      1    2   2   2
'b':   w_1  w_2 w_3  w_4
       5     5   5    5
       6     6   6    6
'c':   w_1   w_2 w_3  w_4
        7      7   7    7
        8      8   8    8
'd':   w_1    w_2  w_3  w_4
..........................
..........................}

All I want to create new dataframes based on existing key names and columns data as dictionary values. Something like:

df_a :

w_1 w_2 w_3 w_4
 3    3   3   3 
 1    2   2   2

df_b :

w_1  w_2 w_3  w_4
5     5   5    5
6     6   6    6

df_c :

w_1   w_2 w_3  w_4
 7      7   7    7
 8      8   8    8

df_d :

w_1   w_2 w_3  w_4
..................
..................
................
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
James
  • 528
  • 1
  • 6
  • 18

1 Answers1

3

It is possible, but not recommended:

for k, v ind.items():
    globals()['df_' + k] = v

for (k1, k2), v ind.items():
    globals()['df_' + str(k1) + str(k2)] = v

Better is select dictionary by keys like:

print (d['a'])
print (d['b'])

If tuples:

print (d[('a1', 4)])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Can you throw some light on why not recommended? (for learnings :) ) Thanks – anky Apr 14 '19 at 08:16
  • 2
    @anky_91 - Give me some time for find it :) – jezrael Apr 14 '19 at 08:17
  • 1
    @anky_91 - added to answer - [link](https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string) – jezrael Apr 14 '19 at 08:39
  • @James - try `df_1.04.0` – jezrael Apr 14 '19 at 08:42
  • 1
    @James - Or `print (d[(1.0, 4.0)])` – jezrael Apr 14 '19 at 08:42
  • @jezrael Actually what I'm trying to do if I have keys in dictionary as (1,1), (1,2),(1,3),(1,4),(1,5) then I want to create new dataframe as d_1 to d_5. (Keys could be more then 5 lets say n). So. in that case it would be d_1, d_2......d_n dataframes. – James Apr 14 '19 at 09:32