简体   繁体   中英

creating a new column based on groupby sum condition and groupby in pandas

I have this dataframe, I need to create a reached_det and reached_web column in Pandas dataframe. The conditions for this is if sum of detail for any particular geo_id > 0, then reached_det for that id is 1. Similarly for reached web.

我有这个数据框,我需要在 Pandas 数据框中创建一个reached_det 和reached_web 列。这样做的条件是,如果任何特定 geo_id > 0 的详细信息总和,则该 id 的reached_det 为1。对于到达的网络也是如此。

The Final data will look like this

在此处输入图像描述

Condition is shown below

在此处输入图像描述

My code in python. It is giving me error. Please help

data['reached_det'] =np.where(data.groupby('geo_id'])['det'].sum()>0,1,0)

Try change it with transform

data['reached_det'] = np.where(data.groupby('geo_id'])['det'].transform('sum')>0, 1, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM