1

I am trying to execute a function multiple times by iterating over a dataframe containing parameters. The parameter dataframe looks like this:

nwaarde = pd.DataFrame({'material':['ST','PVC400mm_na1977'], 
            'n':[1, 3], 
            'datalocation':[16, 49]})

The material column contains the names of the main dataframes. I would like to use these material names to select values from a specific material dataframe so i can use those values in the following function.

def func(x, a, b, c, d):
    return a * x**n + b * np.exp(-n * x/d) + c

Because I have multiple dataframes for this operation i would like to iterate through all the materials. And in this way create different popt and pcov arrays for all the different materials. I have tried this with the following script but failed.

for index, row in nwaarde.iterrows():
    xdata = [row.ix[1]].iloc[:100, 0]
    ydata = [row.ix[1]].iloc[:100, row.ix[0]]
    popt, pcov = curve_fit(func, xdata, ydata, maxfev = 1000000)
    popt_[row.ix[1]] = popt
    pcov_[row.ix[1]] = pcov

This script produces the following error.

'list' object has no attribute 'iloc'

If I execute the statements seperatly it does work.

In [188]: row.ix[1]
Out[188]: 'ST'

In [189]: ST.iloc[:100, 0]
Out[189]: 
0      0
1      1
2      2
3      3
4      4
5      5
6      6
7      7
8      8
9      9
10    10

Is there a way to get the name from the parameter dataframe so I can use this in the same line to select data from a dataframe needed for the function?

Timon
  • 11
  • 2
  • You can try `eval`, but [be careful](https://stackoverflow.com/a/9384005/5037901). It might be better to put your dataframes into a dictionary, then you can access the dataframe by using its key. See [this answer](https://stackoverflow.com/a/15193148/5037901). – juan Oct 03 '17 at 15:57

0 Answers0