How to convert rows values in dataframe to columns labels in Python after groupby?

假如想象 提交于 2019-12-04 19:15:08

You could use pd.pivot

In [171]: df.pivot(index='Schoolname', columns='Attribute', values='Value')
Out[171]:
Attribute   Cleanliness  Money  Safe
Schoolname
abc-School         4.50   4.90  4.40
lmn-School         3.89   4.65  2.34
xyz-School         2.34   4.65  3.44

or more expressible pd.pivot_table

In [172]: pd.pivot_table(df, values='Value', index='Schoolname', columns='Attribute')
Out[172]:
Attribute   Cleanliness  Money  Safe
Schoolname
abc-School         4.50   4.90  4.40
lmn-School         3.89   4.65  2.34
xyz-School         2.34   4.65  3.44
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!