简体   繁体   English

聚合 Function 到 dataframe,同时保留 Pandas 中的行

[英]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 .我想根据一个名为COLLISION_ID的字段和每个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.我想删除重复的 COLLISION_ID,因为它们具有相同的坐标,但保留原始数据集中的出现次数。

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)我希望我的数据以COLLISION_ID数字、计数和我的数据的剩余列的形式返回,这些列未在此处显示(大约 40 列稍后将被过滤)

If you are talking about filter, we should do transform如果您在谈论过滤器,我们应该进行transform

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

Then you can filter the df1 with column count然后您可以使用列数过滤 df1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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