How to count the iteration over a apply function for rows in pandas

If I use the apply to loop a function to each rows in the pandas dataframe like:

def my_function():
    return "True"

df['Result'] = df.apply(lambda row: my_function(row), axis = 1)

How can i count the iteration to track the progress

1 answer

  • answered 2018-11-08 07:26 Kris

    It's a tough one. It kind of depends on what your function does. In particular, if your function can be broadcasted. If so, I believe that that's what Pandas will do. In which case there's no obvious candidate for a "loop counter", i.e. progress indicator.

    If, on the other hand, you want to do a linear pass with some complicated non-broadcastable operation, then you might as well write your for-loop explicitly.