While I think I could do this naively and poorly, I'm interested to learn a more elegant and efficient approach.
Given the following dataframe:
In [42]: df = pd.DataFrame({'flavor':['goober','tronic','goober','tronic'], 'points':[42,55,31,101]}, index=['foo','foo','bar','bar'])
In [43]: df
Out[43]:
flavor points
foo goober 42
foo tronic 55
bar goober 31
bar tronic 101
I would like to groupby the index, and convert values from flavor
column into column headers themselves, completely throwing away the flavor
and points
. So the final result would look like:
In [44]: pd.DataFrame({'goober':[42,31], 'tronic':[55,101]}, index=['foo','bar'])
Out[44]:
goober tronic
foo 42 55
bar 31 101
Thanks for any suggestions.
Use DataFrame.pivot
with convert index to column first and then remove index and columns names by DataFrame.rename_axis
:
df = df.reset_index().pivot('index', 'flavor','points').rename_axis(index=None,columns=None)
print (df)
goober tronic
bar 31 101
foo 42 55
Or use DataFrame.set_index
with Series.unstack
:
df = (df.set_index('flavor', append=True)['points']
.unstack()
.rename_axis(index=None, columns=None))
print (df)
goober tronic
bar 31 101
foo 42 55
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments