Arnold Klein
Arnold Klein

Reputation: 3086

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!

Upvotes: 6

Views: 11318

Answers (1)

piRSquared
piRSquared

Reputation: 294258

Try this:

pd.concat([df.data.apply(pd.Series), df.drop('data', axis=1)], axis=1)

Upvotes: 7

Related Questions