簡體   English   中英

pandas - groupby和re-scale values

[英]pandas - groupby and re-scale values

我想在此數據框中添加一個重新調整的列:

I,Value
A,1
A,4
A,2
A,5
B,1
B,2
B,1

以便新列(讓我們稱之為scale ),在每個I組的value列上跟隨一個函數。 該函數只是每個組范圍內的標准化:

lambda x: (x-min(x))/(max(x)-min(x))

到目前為止我試過:

d = df.groupby('I').apply(lambda x: (x-min(x))/(max(x)-min(x)))

收到以下TypeError:

TypeError: Could not operate array(['A'], dtype=object) with block values index 1 is out of bounds for axis 1 with size 1

如果您將“值”列添加到代碼中,那么它將起作用:

In [69]:
df.groupby('I')['Value'].apply(lambda x: (x-min(x))/(max(x)-min(x)))

Out[69]:
0    0.00
1    0.75
2    0.25
3    1.00
4    0.00
5    1.00
6    0.00
dtype: float64

pandas方法版本如下,它產生相同的結果:

In [67]:
df['Normalised'] = df.groupby('I')['Value'].apply(lambda x: (x-x.min())/(x.max()-x.min()))
df

Out[67]:
   I  Value  Normalised
0  A      1        0.00
1  A      4        0.75
2  A      2        0.25
3  A      5        1.00
4  B      1        0.00
5  B      2        1.00
6  B      1        0.00

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM