5

I'm wondering how to flatten the nested pandas dataframe as demonstrated in the picture attached. enter image description here

The nested attribute is given by 'data' field. In short: I have a list of participants (denoted by 'participant_id') and they submitted responses ('data') at different times. I need to create the wide dataframe, where for each participant at each time stamp there is a row of records of their data ('q1', 'q2',...,'summary')

Many thanks in advance!

Arnold Klein
  • 2,676
  • 9
  • 29
  • 52

1 Answers1

7

Try this:

pd.concat([df.data.apply(pd.Series), df.drop('data', axis=1)], axis=1)
piRSquared
  • 265,629
  • 48
  • 427
  • 571
  • 2
    But the columns names are 0 ,1 and etc. and 0 wil have {"q1": "me"}, Instead can we get column name as "q1" and that value as "me" ? @piRSquared – 7H3 IN5ID3R Aug 16 '17 at 11:49