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
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
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'])
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.
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.