i have this code:
import pandas as pd
from itertools import product
for a, b, c, d, e in product(range(x), range(y), range(z), range(t), range(m):
factor = foo(a, b, c, d, e)
result_df.loc[len(result_df.index)] = [a, b, c, d, e, factor]
In which I use itertools.product to generate 5 variables and then use those 5 in a foo function. then append the results to a dataframe.
the foo function is fully optimized and uses vectorization and numpy in every calculation.
is there any way to make this code run faster?
Edit: so apparently using df.loc to append is very slow. what do you suggest? how can I store every iteration's a,b,c,d,e & factor, and then make a dataframe out of it?
Edit 2: So as other guys mentioned I used list append instead of appending to dataframe, then made a dataframe out of the list at the end. with below code:
import pandas as pd
from itertools import product
res_list=[]
for a, b, c, d, e in product(range(x), range(y), range(z), range(t), range(m):
factor = foo(a, b, c, d, e)
res_list.append([a, b, c, d, e, factor])
col_list = ['x', 'y', 'z', 't', 'm', 'factor']
res_df = pd.DataFrame(res_list, columns=col_list)
and its faster now, but not that much faster. its roughly 10% quicker. any other tips?