python如何给某列数据打标签,分组后如何将数据框中的行值转换为Python中的列标签?...

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

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

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.

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值