Why does pandas apply calculate twice

This behavior is intended, as an optimization.

See the docs:

In the current implementation apply calls func twice on the first
column/row to decide whether it can take a fast or slow code path.
This can lead to unexpected behavior if func has side-effects, as they
will take effect twice for the first column/row.

Leave a Comment