Skip to content Skip to sidebar Skip to footer

How To Do A Transpose A Dataframe Group By Key On Pandas?

I have this table from my database and I need a transpose group by survey_id id answer survey_id question_number questionid 216 0.0 69 3 2.0

Solution 1:

You can use pivot, add_prefix and reset_index:

print (df.pivot(index='survey_id', columns='question_number', values='answer')
         .add_prefix('P')
         .reset_index())

question_number  survey_id   P3   P4   P5   P6   P8
0                       69  0.0  3.0  0.0  0.0  0.0

Post a Comment for "How To Do A Transpose A Dataframe Group By Key On Pandas?"