1

I have a multi-level dictionary called mydict that looks like the one bellow and I am trying to do a concatenation that gives me a different dataframe for each keys of the dict.

mydict -> {agent_1: {step1: {...}, step2: {...}, step3: {...}}, ...}

I can do that with the following code but it means doing it by hand and I am working with large data.

boo  = pd.DataFrame.from_dict(mydict[agent_1]['step_1'])
boo1 = pd.DataFrame.from_dict(mydict[agent_1]['step_2'])
boo2 = pd.DataFrame.from_dict(mydict[agent_1]['step_3'])
boo3 = pd.DataFrame.from_dict(mydict[agent_1]['step_4'])

boo_tot = pd.concat([boo, boo1, boo2, boo3])

Also I have troubles automatizing this because the number of step parameter is not the same for each key of mydict. As an example, let's say agent_1 has 4 steps while agent_2 only has 3. I tried following what was given in this stackoverflow accepted answer but it does not apply to my situation.

As you can see bellow, this method gives me one dataframe and all the values for each step (= etape in the df bellow) are in one row each time whereas I would like a different dataframe for each agent with pcc, time, route_type and mode as columns and their values printed on row under the other for each step

enter image description here

This is what I was hoping to get instead:

For agent_1:

    mode    pcc     time    route_type  etape    id
0   TC      data1   time1   data1       etape_0  NaN
1   TC      data2   time2   data2       etape_0  NaN
2   TC      data3   time3   data3       etape_0  NaN
3   TC      data4   time4   data4       etape_0  NaN
... ...     ...     ...     ...         ...      ...
16  MAP     data16  time16  data16      etape_1  NaN
17  MAP     data17  time17  data17      etape_1  NaN
18  MAP     data18  time18  data18      etape_1  NaN
19  MAP     data19  time19  data19      etape_1  NaN
... ...     ...     ...     ...         ...      ...

And then I'd get a second dataframe for agent_2, a third for agent_3 etc.

I couldn't find a way to get what I want and unfortunately my vocabulary about this subject is very limited since I don't use multi-level dicts often. Any tips on the matter would be a huge help.

I.M.
  • 344
  • 3
  • 14

0 Answers0