pandas - groupby 和重新调整值 [英] pandas - groupby and re-scale values
本文介绍了pandas - groupby 和重新调整值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想向这个数据框添加一个重新缩放的列:
I would like to add a rescaled column to this dataframe:
I,Value
A,1
A,4
A,2
A,5
B,1
B,2
B,1
以便新列(我们称之为scale
)在每个I
组的value
列上遵循一个函数.该函数只是对每个组的范围进行归一化:
so that the new column (let's call it scale
), follows a function over the value
column for each group of I
. The function is just a normalization over the range for each group:
lambda x: (x-min(x))/(max(x)-min(x))
到目前为止我尝试过:
d = df.groupby('I').apply(lambda x: (x-min(x))/(max(x)-min(x)))
收到以下类型错误:
TypeError: Could not operate array(['A'], dtype=object) with block values index 1 is out of bounds for axis 1 with size 1
推荐答案
如果您将值"列添加到您的代码中,那么它会起作用:
If you added the 'Value' column to your code then it would work:
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 方法版本如下,结果相同:
The pandas method version is the following which produces the same result:
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
这篇关于pandas - groupby 和重新调整值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文