Pandas groupby in combination with sklean preprocessing continued

走远了吗. 提交于 2021-02-07 20:24:11

问题


Continue from this post: Pandas groupby in combination with sklearn preprocessing

I need to do preprocessing by scaling grouped data by two columns, somehow get some error for the second method

import pandas as pd
import numpy as np
from sklearn.preprocessing import robust_scale,minmax_scale

df = pd.DataFrame( dict( id=list('AAAAABBBBB'),
                loc = (10,20,10,20,10,20,10,20,10,20),
                value=(0,10,10,20,100,100,200,30,40,100)))

df['new'] = df.groupby(['id','loc']).value.transform(lambda x:minmax_scale(x.astype(float) ))

df['new'] = df.groupby(['id','loc']).value.transform(lambda x:robust_scale(x ))

The second one give me error like this:

ValueError: Expected 2D array, got 1D array instead: array=[ 0. 10. 100.]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

If I use reshape I got error like this:

Exception: Data must be 1-dimensional

If I ever print out the grouped data, g['value'] is pandas series.

for n, g in df.groupby(['id','loc']):
    print(type(g['value']))

Do you know what might cause it?

Thanks.


回答1:


Base on the warning code , you should add reshape and concatenate

df.groupby(['id','loc']).value.transform(lambda x:np.concatenate(robust_scale(x.values.reshape(-1,1))))
Out[606]: 
0   -0.2
1   -1.0
2    0.0
3    1.0
4    1.8
5    0.0
6    1.0
7   -2.0
8   -1.0
9    0.0
Name: value, dtype: float64


来源:https://stackoverflow.com/questions/54245551/pandas-groupby-in-combination-with-sklean-preprocessing-continued

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!