I\'ve seen a few variations on the theme of exploding a column/series into multiple columns of a Pandas dataframe, but I\'ve been trying to do something and not really succe
There are a few ways:
using .pivot_table:
>>> df.pivot_table(values='val', index=df.index, columns='key', aggfunc='first')
key bar baz foo
id
2 bananas apples oranges
3 kiwis NaN grapes
using .pivot:
>>> df.pivot(index=df.index, columns='key')['val']
key bar baz foo
id
2 bananas apples oranges
3 kiwis NaN grapes
using .groupby followed by .unstack:
>>> df.reset_index().groupby(['id', 'key'])['val'].aggregate('first').unstack()
key bar baz foo
id
2 bananas apples oranges
3 kiwis NaN grapes