Reputation: 51
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?
Upvotes: 2
Views: 40
Reputation: 120409
You missed the aggfunc
parameter to pivot_table
but you have to make a choice on how to treat duplicate values. Below, I choose first
:
>>> df.pivot_table(index='id', columns='question', values='answer', aggfunc='first')
question a b
id
1 aa bb
2 aa bb
3 aa bb
Upvotes: 1