Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何按列分组并进行规范化?_Python 3.x_Pandas_Aggregate_Pandas Groupby - Fatal编程技术网

Python 3.x 如何按列分组并进行规范化?

Python 3.x 如何按列分组并进行规范化?,python-3.x,pandas,aggregate,pandas-groupby,Python 3.x,Pandas,Aggregate,Pandas Groupby,假设我有这样一个日期框: A B C 0 foo one 1 1 bar one 2 2 foo two 1 3 bar three 2 4 foo two 3 5 bar two 5 6 foo one 2 7 foo three 5 8 bar one 4 A B C 0

假设我有这样一个日期框:

    A      B      C       
0  foo    one     1
1  bar    one     2
2  foo    two     1
3  bar  three     2
4  foo    two     3
5  bar    two     5
6  foo    one     2
7  foo  three     5
8  bar    one     4
    A      B      C       
0  foo    one    0.25
1  bar    one    0.5
2  foo    two    0.2
3  bar  three    0.25
4  foo    two    0.6
5  bar    two     1
6  foo    one    0.5
7  foo  three     1
8  bar    one     1
我想按“B”分组,并对每个特定的“B”在“C”列上进行规范化。 我想做一个简单的minmax范数,比如x/max(x)

结果如下所示:

    A      B      C       
0  foo    one     1
1  bar    one     2
2  foo    two     1
3  bar  three     2
4  foo    two     3
5  bar    two     5
6  foo    one     2
7  foo  three     5
8  bar    one     4
    A      B      C       
0  foo    one    0.25
1  bar    one    0.5
2  foo    two    0.2
3  bar  three    0.25
4  foo    two    0.6
5  bar    two     1
6  foo    one    0.5
7  foo  three     1
8  bar    one     1
用于返回
系列
,大小与原始的
df
相同:

grouped_b = df.groupby('B')

def norm(value):
    return value/value.max()

df['C'] = grouped_b['C'].transform(norm)

print (df)
     A      B     C
0  foo    one  0.25
1  bar    one  0.50
2  foo    two  0.20
3  bar  three  0.40
4  foo    two  0.60
5  bar    two  1.00
6  foo    one  0.50
7  foo  three  1.00
8  bar    one  1.00
您还可以使用
lambda
功能:

df['C'] = df.groupby('B')['C'].transform(lambda x: x / x.max())

使用
transform

df.C/=df.groupby(['B']).C.transform('max')

@志成国-不客气!如果我的回答有帮助,别忘了。谢谢