简体   繁体   中英

Aggregate Function to dataframe while retaining rows in Pandas

I want to aggregate my data based off a field known as COLLISION_ID and a count of each COLLISION_ID .

I want to remove repeating COLLISION_IDs since they have the same Coordinates, but retain a count of occurrences in original data-set.

My code is below

df2 = df1.groupby(['COLLISION_ID'])[['COLLISION_ID']].count()

This returns such: 在此处输入图像描述

I would like my data returned as the COLLISION_ID numbers, the count, and the remaining columns of my data which are not shown here(~40 additional columns that will be filtered later)

If you are talking about filter, we should do transform

df1['count_col']=df1.groupby(['COLLISION_ID'])['COLLISION_ID'].transform('count')

Then you can filter the df1 with column count

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