Pandas GroupBy.apply method duplicates first group

This is by design, as described here and here

The apply function needs to know the shape of the returned data to intelligently figure out how it will be combined. To do this it calls the function (checkit in your case) twice to achieve this.

Depending on your actual use case, you can replace the call to apply with aggregate, transform or filter, as described in detail here. These functions require the return value to be a particular shape, and so don’t call the function twice.

However – if the function you are calling does not have side-effects, it most likely does not matter that the function is being called twice on the first value.

Leave a Comment