I am using a nested for loop to read selective data from a dataframe. And then I need to implement some mathematical formulae to that selective data. For this reason, I implemented a dynamic logic that separated out the index and column numbers of the data in a list "idx" and "cols". But the nested For Loop that I have applied to read this data is executing for unexpectedly greater number of times.
Following is the sample code and its output:
idx = [1, 2]
cols = [2, 2]
count = 0
def run_imputation():
global count
for i in idx:
for col in cols:
count += 1
print(count)
dfClean.iloc[i, col] = tempOut[i,col] #Need to do such and more computations
origVal.append(dfClean_Orig.iloc[i, col])
impuVal.append(dfClean.iloc[i, col])
%timeit run_imputation()
OUTPUT:
1
2
......
32444
So my question is that why is For loop executing 32444 times, whereas it should just execute 4 times. And is there any better way for doing such selective data computations as shown below other than such complicated For Loops in Python?