[英]pandas dataframe groupby index and convert row values into columns
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
.我想对索引进行分组,并将
flavor
列中的值转换为列标题本身,完全丢弃flavor
和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
:首先使用
DataFrame.pivot
将索引转换为列,然后通过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
:或者使用
DataFrame.set_index
和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
You can use pandas.pivot_table
:您可以使用
pandas.pivot_table
:
print( pd.pivot_table(df, index=df.index, columns='flavor', values='points') )
Prints:印刷:
flavor goober tronic
bar 31 101
foo 42 55
Note: If you don't need aggfunc
, use .pivot()
instead.注意:如果您不需要
aggfunc
,请改用.pivot()
。 As @jezrael pointed out in the comments.正如@jezrael 在评论中指出的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.