我很难将agg
应用到groupby
pandas数据帧。在
我有一个数据帧df
,如下所示:
order_id distance_theo bird_distance
10 100 80
10 80 80
10 70 80
11 90 70
11 70 70
11 60 70
12 200 180
12 150 180
12 100 180
12 60 180
我想按order_id
分组,通过将每组第一行的distance_theo
除以每组第一行中的bird_distance
得到一个新的列crow
(因为一个组中只有一个值bird_distance
)。在
我的尝试: 数据框groupby('order_id').agg({'crow',lambda x:x.distance)_西奥·海德(1) /x.伯德_距离.头部(1) })
但我得到一个错误:
'Series' object has no attribute 'distance_theo'
我怎么解决这个问题?谢谢你的任何建议!在
您可以不使用groupby并使用}:
drop_duplicate
和{或者使用}根据@jezraela下面的评论:
^{pr2}$assign
而不是{输出:
将
groupby
与first
一起使用:相关问题 更多 >
编程相关推荐