0

I have a dictionary which contains dictionaries which contain dataframes

dict
key value
s0   sub_dict1
s1   sub_dict2

sub_dict1
key value
x1  df1
x2  df2


df1
time val other_data_i_don't_want
0:01  1       999
0:34  2       999

What I ultimately want is a dictionary which contains dataframes as the values

dict_new
key value
s0  df

where df

time val_x1 val_x2 val_x3 
0:01   1         1       1 

and i want to automate this process in a for loop

so far i have done:

for key1 in dict.keys():
 for key2 in dict[key1].keys():
 global key1_key2
 sensor = str(key2)
 key1_key2 = dict[key1][key2][['time','val']]
 key1_key2.rename(columns = {"val":'val_'+key2})

but key1_key2 is being defined to have the name "key1_key2" but I actually want to create multiple dataframes named as "s0_x1" "s0_x2" "s1_x1" "s1_x2"

and the rename statement also doesn't work.

once I have the "s0_x1" etc i can do a full outer join to create the df I want. I just can't figure out how to define these as global variables in the first instance

Laura
  • 177
  • 1
  • 12
  • I would try to avoid defining global variables like that if possible. https://stackoverflow.com/a/5036775/7619380 – joshwilsonvu Jun 24 '19 at 17:28
  • I want to create multiple variable so I can merge them together. The only stuff about merging dictionaries i've found overwrites the information, but i want to a full outer join. – Laura Jun 24 '19 at 17:46

1 Answers1

0

Instead of global variables, can you create a dict of dicts instead? Something like

dataframes = {}
for sN, sub_dictN in dict.items():
  for xN, dfN in sub_dictN.items():
    current_dataframe = '{}_{}'.format(sN, xN)
    dataframes[current_dataframe] = {}  # instead of global
    # fill the dataframe with data as you like

joshwilsonvu
  • 2,569
  • 9
  • 20