Skip to content
Advertisement

How to efficiently do operation on pandas each group

So I have a data frame like this–

JavaScript
JavaScript

What I am doing is grouping by id and doing rolling operation on the delay column like below–

JavaScript
JavaScript

It is working just fine but I am curious whether .apply on grouped data frame is vectorized or not. Since my dataset is huge, is there a better-vectorized way to do this kind of operation? Also I am curious if Python is single-threaded and I am running on CPU how pandas, numpy achieve vectorized calculation.

Advertisement

Answer

You can use strides for vectorized rolling with GroupBy.transform:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement