In [134]: a=[1,2,3]
In [135]: b=[4,5,6]
Your list of 'indices' and values:
In [140]: alist = []
In [142]: for i,j in itertools.product(a,b):
...: v = i*2 + j*.5
...: alist.append([i,j,v])
...:
In [143]: alist
Out[143]:
[[1, 4, 4.0],
[1, 5, 4.5],
[1, 6, 5.0],
[2, 4, 6.0],
[2, 5, 6.5],
[2, 6, 7.0],
[3, 4, 8.0],
[3, 5, 8.5],
[3, 6, 9.0]]
A 3 column dataframe from that:
In [144]: df = pd.DataFrame(alist, columns=['a','b','value'])
In [145]: df
Out[145]:
a b value
0 1 4 4.0
1 1 5 4.5
2 1 6 5.0
3 2 4 6.0
4 2 5 6.5
5 2 6 7.0
6 3 4 8.0
7 3 5 8.5
8 3 6 9.0
One way of using the same data to make 'grid' dataframe:
In [147]: pd.DataFrame(np.array(alist)[:,2].reshape(3,3), columns=a, index=b)
Out[147]:
1 2 3
4 4.0 4.5 5.0
5 6.0 6.5 7.0
6 8.0 8.5 9.0
Oops that maps the rows and columns wrong; lets transpose the 3x3 array:
In [149]: pd.DataFrame(np.array(alist)[:,2].reshape(3,3).T, columns=a, index=b)
Out[149]:
1 2 3
4 4.0 6.0 8.0
5 4.5 6.5 8.5
6 5.0 7.0 9.0
I know numpy
well; my experience with pandas
is limited. I'm sure there are other ways of constructing such a frame. My guess is that if your value function is complex enough, the iteration mechanism will have a minor effect on the overall run time. Simply evaluating your function for each cell will take up most of the time.
If your function can be written to take arrays, rather than scalars, then the values can be easily calculated with out iteration. For example:
In [171]: I,J = np.meshgrid(b,a,indexing='ij')
In [172]: X = J*2 + I*.5
In [173]: X
Out[173]:
array([[4. , 6. , 8. ],
[4.5, 6.5, 8.5],
[5. , 7. , 9. ]])
In [174]: I
Out[174]:
array([[4, 4, 4],
[5, 5, 5],
[6, 6, 6]])
In [175]: J
Out[175]:
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])