2

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

Kevin Ling
  • 540
  • 2
  • 7
  • 15
  • within your function you can just get the index of the current row you're working with, which should correlate with the current iteration. alternatively, you can just iterrows over the dataframe, in which case you will know which row you're currently on – Simon Nov 08 '18 at 06:14
  • 1
    Another possible solution is [this](https://stackoverflow.com/a/50477710), but maybe need [Progress indicator during pandas operations (python)](https://stackoverflow.com/q/18603270) – jezrael Nov 08 '18 at 06:18

3 Answers3

4

I figured out a workaround for this, if you are using a dataframe then add a counter like this

input_df['counter']=0

for i,row in input_df.iterrows():
    input_df['counter'][i]= i+1

Your Apply statement:-

input_df.apply(YourFunction,axis=1)

Your Calling function:-

def YourFunction(row):
    print(row['counter'])
1

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.

Kris
  • 22,079
  • 3
  • 30
  • 35
0

I was also facing the same issue.

df['index'] = df.index 
def my_function(count):
    if count%5==0:
        print(count)
    return "True"


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

You can simply print the index, till where your code has been executed.

Prachi
  • 1
  • 1
  • 1