groupby之后,如何将数据框中的行值转换为Python中的列标签? [英] How to convert rows values in dataframe to columns labels in Python after groupby?
本文介绍了groupby之后,如何将数据框中的行值转换为Python中的列标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有特定情况要转换此df: 打印df
I have specific case where I want to convert this df: print df
Schoolname Attribute Value
0 xyz School Safe 3.44
1 xyz School Cleanliness 2.34
2 xyz School Money 4.65
3 abc School Safe 4.40
4 abc School Cleanliness 4.50
5 abc School Money 4.90
6 lmn School Safe 2.34
7 lmn School Cleanliness 3.89
8 lmn School Money 4.65
我需要采用这种格式,以便将其转换为numpy数组以进行线性回归建模.
Which i need to get in this format so that i can convert it to numpy array for linear regression modelling.
required_df:
Schoolname Safe Cleanliness Money
0 xyz School 3.44 2.34 4.65
1 abc School 4.40 4.50 4.90
2 lmn School 2.34 3.89 4.65
我知道我们需要做groupby('Schoolname'),但是之后就无法想到要让行名成为列标签以及对应的值反映在required_df中.
I know we need to do groupby('Schoolname') but unable to think after that to get rows name to become column label and corresponding values reflected in required_df.
我需要这种格式,以便可以将其转换为numpy数组,并将其作为我的X向量提供给线性回归模型.
I need in this format so that I can convert it to numpy array and give it to Linear Regression model as my X vector.
推荐答案
您可以使用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
或更可表达的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
这篇关于groupby之后,如何将数据框中的行值转换为Python中的列标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文