Pandas - transpose one column
Since you aren't performing an aggregation, pd.DataFrame.pivot
should be preferred to groupby
/ pivot_table
:
res = df.pivot(index='date', columns='name', values='quantity')
print(res)
name A B C
date
1/1/2018 5 6 7
1/2/2018 9 8 6
If you wish you can use reset_index
to elevate date
to a column.
By no means is my proposed solution better than jpp's. I just happened to run into the same problem and solved it differently.
df.set_index(['date', 'name']).unstack()
The result looks a little messier too but it worked in my case: