Good Evening. I need help for a question in Python to reshape a Dataframe.
I am trying to reshape a dataframe like this:
df
| id | question | answer |
|---|---|---|
| 1 | a | aa |
| 1 | b | bb |
| 2 | a | aa |
| 2 | b | bb |
| 3 | a | aa |
| 3 | b | bb |
For this:
df_result
| id | a | b |
|---|---|---|
| 1 | aa | bb |
| 2 | aa | aa |
| 3 | bb | bb |
I tried with:
df.pivot(index='id', columns = 'question', values = 'answer')
But return : Index contains duplicate entries, cannot reshape.
I tried with pandas.melt and pandas.pivot_table, but both went wrong.
How I can resolve this?