how to use pandas to standardlize data while groupby the result by another columns

Viewed 30

The goal is to group the data by columns type, then within the groups identified, use Min-Max standardization to process the column called score and assign to a new column called normalization_score

import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()

here is the df looks like

enter image description here

here is the first try:

df["normalization_score"] = df.groupby(["type"]).apply(scaler.fit_transform(df["score"]))

and raise ValueError

ValueError: Expected 2D array, got 1D array instead:
array=[0.28571429 0.01639344 0.42857143 0.14285714 0.57142857 0.71428571
 0.16393443 0.08196721 0.06557377 0.03278689 0.1147541  0.09836066
 0.13114754 0.18032787 0.27868852 0.21311475 0.36065574 0.14754098
 0.32786885 0.04347826 0.04918033 0.08695652 0.40983607 0.85714286
 0.42622951 0.3442623  0.29508197 0.2295082  0.24590164 0.67213115
 0.59016393 0.19672131 0.13043478 0.45901639 0.44262295 0.17391304
 0.50819672 0.54098361 0.47540984 0.2173913  0.52459016 0.26086957
 0.49180328 0.63934426 0.37837838 0.60655738 0.72131148 0.26229508
 0.40540541 0.57377049 0.08108108 1.         0.62295082 0.6557377
 0.55737705 0.68852459 0.13513514 0.27027027 0.59459459 0.34782609
 0.24324324 0.52173913 0.73770492 0.05405405 0.02702703 0.43478261
 0.43243243 0.30434783 0.7704918  0.60869565 0.51351351 0.78688525
 0.2972973  0.10810811 0.48648649 0.35135135 0.18918919 0.80327869
 0.56756757 0.73913043 0.69565217 0.81967213 0.32432432 0.65217391
 0.7826087  0.75409836 0.83606557 0.7027027  0.7027027  0.7826087
 0.7826087  0.7027027  0.83606557 0.56521739 0.7826087  0.45945946
 0.31147541 0.83606557 0.16216216 0.7826087  0.83606557 0.62162162
 0.7027027  0.7027027  0.39344262 0.37704918 0.7027027  0.47826087
 0.83606557 0.7826087  0.83606557 0.7027027  0.67567568 0.7027027
 0.83606557 0.83606557 0.83606557 0.39130435 0.7027027  0.83606557
 0.21621622 0.70491803 0.83606557 0.54054054 0.7027027  0.64864865
 0.7027027  0.7027027 ].
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.

then i try this code followed by instruction

df["normalization_score"] = df.groupby(["type"]).apply(scaler.fit_transform(np.array(df["score"]).reshape(-1, 1)))

and it shows this error

File ~/opt/anaconda3/envs/pythonProject/lib/python3.10/site-packages/pandas/core/groupby/groupby.py:1248, in GroupBy.apply(self, func, *args, **kwargs)
   1241 @Appender(
   1242     _apply_docs["template"].format(
   1243         input="dataframe", examples=_apply_docs["dataframe_examples"]
   1244     )
   1245 )
   1246 def apply(self, func, *args, **kwargs):
-> 1248     func = com.is_builtin_func(func)
   1250     # this is needed so we don't try and wrap strings. If we could
   1251     # resolve functions to their callable functions prior, this
   1252     # wouldn't be needed
   1253     if args or kwargs:

File ~/opt/anaconda3/envs/pythonProject/lib/python3.10/site-packages/pandas/core/common.py:582, in is_builtin_func(arg)
    577 def is_builtin_func(arg):
    578     """
    579     if we define an builtin function for this argument, return it,
    580     otherwise return the arg
    581     """
--> 582     return _builtin_table.get(arg, arg)

TypeError: unhashable type: 'numpy.ndarray'
1 Answers

You can use minmax_scale:

from sklearn.preprocessing import minmax_scale

df['norm_score'] = df.groupby('type')['score'].transform(minmax_scale)
print(df)

# Output
   type     score  norm_score
0     a  0.848994    0.866129
1     a  0.876353    0.895023
2     a  0.295306    0.281368
3     a  0.792014    0.805951
4     a  0.975751    1.000000
5     a  0.533664    0.533102
6     a  0.028889    0.000000
7     a  0.784051    0.797541
8     a  0.297865    0.284071
9     a  0.049603    0.021876
10    b  0.122400    0.000000
11    b  0.660356    0.661763
12    b  0.189777    0.082882
13    b  0.444888    0.396706
14    b  0.626786    0.620467
15    b  0.821723    0.860267
16    b  0.182170    0.073525
17    b  0.554681    0.531768
18    b  0.305112    0.224761
19    b  0.935313    1.000000
Related