[英]Pandas groupby apply customized function to each group
I have a table with multiple columns.我有一个包含多列的表。 For one of the columns AC , I need to replace it with Result AC as shown in the table below
对于其中一列AC ,我需要将其替换为Result AC ,如下表所示
The function has a pseudo code as below: function的伪代码如下:
For each unique address:
If
# of unique(AC) < Value in column B AND
len(Top 1 (unique(AC))) != len(Top 2 (unique(AC))):
return mode(air_conditioning)
Else
For each unique(AC), calculate sum(area)
If Top 1 (sum(area)) > Top 2 (SUM(area)):
return air_conditioning which has Top 1 (SUM(building_area))
Else
For each unique(AC), SUM(value)
return air_conditioning with Top 1 (SUM(value))
Groupby, transfrom sum should do Groupby,transfrom sum 应该做
df['col2']=df.groupby('col1')['col2'].transform('sum')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.