6

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,956
  • 10
  • 31
  • 60

1 Answers1

7

Try this:

pd.concat([df.data.apply(pd.Series), df.drop('data', axis=1)], axis=1)
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • 3
    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 – gwthm.in Aug 16 '17 at 11:49