Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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 数据帧在一个函数中将每组除以其最大值_Python_Pandas_Pandas Groupby - Fatal编程技术网

Python 数据帧在一个函数中将每组除以其最大值

Python 数据帧在一个函数中将每组除以其最大值,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我想将每一行除以它在每个组中的最大值。我做了一个专栏,但我想做的高度和距离也。我如何在一个函数中完成它 data = [['1020',12000,100,1.5], ['1020',15000,120,0.9], ['1020',13000,90,0.8], ['1020',10000,110,1], ['1030',5000,150,1.2], ['1030',8000,160,1.4], ['1030',7000,140,1.1]

我想将每一行除以它在每个组中的最大值。我做了一个专栏,但我想做的高度和距离也。我如何在一个函数中完成它

data = [['1020',12000,100,1.5],
     ['1020',15000,120,0.9],
     ['1020',13000,90,0.8],
     ['1020',10000,110,1],
     ['1030',5000,150,1.2],
     ['1030',8000,160,1.4],
     ['1030',7000,140,1.1],
     ['1000',20000,160,1.4],
     ['1000',40000,140,1.1],
    ] 
    df = pd.DataFrame(data, columns = ['Group_Id', 
    'Price','Height','Distance']) 

    # print dataframe. 
   df
低于我所尝试的。但是,我如何编写以将此函数合并为三列呢

   dist =  df.groupby('Group_Id')['Price'].transform('max')
   df['Price_score'] = dist.sub(df['Price']).div(dist)
   df.head(10)

在不访问任何
列的情况下使用
groupby.transform
,如下所示:

dist =  df.groupby('Group_Id').transform('max')
df.loc[:, df.drop('Group_Id', axis=1).columns] = (dist.sub(df.drop('Group_Id', axis=1))
                                                      .div(dist))

print(df)
  Group_Id     Price    Height  Distance
0     1020  0.200000  0.166667  0.000000
1     1020  0.000000  0.000000  0.400000
2     1020  0.133333  0.250000  0.466667
3     1020  0.333333  0.083333  0.333333
4     1030  0.375000  0.062500  0.142857
5     1030  0.000000  0.000000  0.000000
6     1030  0.125000  0.125000  0.214286
7     1000  0.500000  0.000000  0.000000
8     1000  0.000000  0.125000  0.214286
或者,如果要保留原始列,请使用:

dist =  df.groupby('Group_Id').transform('max')
df = df.join(dist.sub(df.drop('Group_Id', axis=1)).div(dist).add_suffix('_grp'))

print(df)
  Group_Id  Price  Height  Distance  Price_grp  Height_grp  Distance_grp
0     1020  12000     100       1.5   0.200000    0.166667      0.000000
1     1020  15000     120       0.9   0.000000    0.000000      0.400000
2     1020  13000      90       0.8   0.133333    0.250000      0.466667
3     1020  10000     110       1.0   0.333333    0.083333      0.333333
4     1030   5000     150       1.2   0.375000    0.062500      0.142857
5     1030   8000     160       1.4   0.000000    0.000000      0.000000
6     1030   7000     140       1.1   0.125000    0.125000      0.214286
7     1000  20000     160       1.4   0.500000    0.000000      0.000000
8     1000  40000     140       1.1   0.000000    0.125000      0.214286


谢谢,但我想保留价格、高度、距离栏。“我该怎么做呢?”梅利克在逻辑上说,你是先减后除。如果您只想除法,则删除逻辑中的减法。
print(dist)
   Price  Height  Distance
0  15000     120       1.5
1  15000     120       1.5
2  15000     120       1.5
3  15000     120       1.5
4   8000     160       1.4
5   8000     160       1.4
6   8000     160       1.4
7  40000     160       1.4
8  40000     160       1.4