I would like to fill single cells of a Pandas Dataframe with 1D-arrays (or lists, whatever works). What I am trying to accomplish, is running over the values in one column and performing some action, that results in an array, which I would like to store in another column of that DataFrame. I think, it is not possible (or very inconvenient) to do this with a lambda function because I think it is way too long for an in-line loop.
For every value of OvenTemp, another function "sample" creates a lot of 3D-coordinates, where NNdist(samples) gives a list of the Next-Neighbour-distances for each of the 3D-coordinates. There is some averaging over different samples, which should not make a big difference for the coding; in the end, there is a long 1D-numpy-array of float numbers, that is holding all the NN-distances. This list I would like to store now in column "NNDistList" and the respective row for the OvenTemp. The NNDistList will always have a different length, since the external function "sample" is creating ensembles of coordinates of different magnitude.
The code looks like this:
distances=pd.DataFrame({'OvenTemp':OvenTemp,'NNDistList':np.zeros(len(OvenTemp))})
for i in distances.OvenTemp:
avgrun=1
avgresult=np.array([])
while avgrun<=averaging:
samples=sample(df.Radius[distances.OvenTemp==i].values[0],df.Dopants[df.OvenTemp==i].values[0])
NNresult=NNdist(samples)
avgresult=np.append(avgresult,NNresult)
avgrun+=1
distances.loc[distances.OvenTemp==i,'NNDistList']=avgresult
Running this code, I obtain the error "Must have equal len keys and value when setting with an iterable".
I tried to use a wrapper for the last line, as suggested here, so it would read
distances.loc[distances.OvenTemp==i,'NNDistList']=[avgresult]
but the error stays the same. The example given in the answer of the old question, that is creating a new DataFrame and filling it with one numpy-array as one cell, works for me. However, adding/replacing one element in an already existing DataFrame by a numpy-array (my code) is still producing this error.
I would appreciate any workaround for this problem, but also any advice on how this could be in general coded in a more efficient way.
Thank you very much, lepakk