1

I have extracted the data of interest from files into a dictionary which contains a dictionary. Now I want to unpack the nested dictionary to make a bigger dataframe that I can analyze. This is my data:

sample = { 'name' : ['cake', 'plum', 'jam'], 'model' : [ 'model_A', 'model_A', 'model_C'], 'data' : [ { 'one': ['A','B', 'C'], 'two': [ 1, 2, 3] }, { 'one': [ 'D', 'E', 'F'], 'two': [ 4, 5, 6]}, {'one': ['G', 'H', 'I'], 'two': [ 7, 8, 9] }]}

I have tried a variety of unpacking methods, including turning the dictionary into slices of pd.Series, and I just can't find anything efficient or working.

This is the dataframe I want:

    'name'   'model'    'one'  'two'
0   'cake'  'modle_A'   'A'     1
1   'cake'  'modle_A'   'B'     2
2   'cake'  'modle_A'   'C'     3
3   'plum'  'modle_A'   'D'     4
4   'plum'  'model_A'   'E'     5
5   'plum'  'model_A'   'F'     6
6   'jam'   'model_C'   'G'     7
7   'jam'   'model_C'   'H'     8
8   'jam'   'model_C'   'J'     9
MelBel88
  • 165
  • 1
  • 1
  • 6
  • Do this then follow guidance from dupe target `df = pd.DataFrame(sample).pipe(lambda d: d.join(pd.io.json.json_normalize(d.pop('data'))))` – piRSquared May 30 '19 at 20:58
  • Cool. I follow your logic. I will study this. Thanks! – MelBel88 May 30 '19 at 21:12
  • @piRSquared, IMO, the dup is bogus. The other question is a straightforward expansion, whereas this one has a trickier internal mapping within the data structure. – Mike May 30 '19 at 21:25
  • Mike, you have a point. But the dup target is directly applicable after applying what I've posted. I felt the most useful to others is the exploding component. If I'm wrong, I've provided the missing bit in the comments. If you are compelled to do so, you should vote to reopen. – piRSquared May 30 '19 at 21:36

0 Answers0