gpt4 book ai didi

python - Pandas groupby 将自定义功能应用于每个组

转载 作者:行者123 更新时间:2023-12-02 02:20:01 25 4
gpt4 key购买 nike

我有一个包含多列的表。对于其中一列AC,我需要将其替换为Result AC,如下表所示

enter image description here

该函数的伪代码如下:

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,从总和转换应该做

df['col2']=df.groupby('col1')['col2'].transform('sum')

关于python - Pandas groupby 将自定义功能应用于每个组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66572462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com