Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten nested pandas dataframe

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!

like image 693
Arnold Klein Avatar asked Jul 25 '16 18:07

Arnold Klein


1 Answers

Try this:

pd.concat([df.data.apply(pd.Series), df.drop('data', axis=1)], axis=1)
like image 142
piRSquared Avatar answered Sep 16 '22 22:09

piRSquared