Reputation: 5
I would like to store generate results from a function that I created called "surface_coverage". The function returns result in numpy.ndarray form and I tried to store it into a dataframe but I kept getting error msg "Must pass 2-d input" here is my code
T = [400,500,600,700]
result = []
for t in df_G['Temperature']:
for i in T:
columns = f'G_ads_{i}'
G_CO2 = df.loc[df.Metal_Oxides == "Al2O3"].loc[df.Adsorbates == 'CO2'][columns].min()
G_H2O = df.loc[df.Metal_Oxides == "Al2O3"].loc[df.Adsorbates == 'H2O'][columns].min()
G_O2 = df.loc[df.Metal_Oxides == "Al2O3"].loc[df.Adsorbates == 'O2'][columns].min()
if t == i+273.15:
theta = surface_coverage(t,P,G_CO2,G_H2O,G_O2,x_co2,x_h2o,x_o2)
result.append(theta)
new_data = pd.DataFrame(result)
and here is the output:
[array([[8.53931326e-04],
[9.34890812e-15],
[9.99146054e-01],
[1.46447007e-08]]), array([[1.07403011e-01],
[4.44545478e-13],
[8.92596825e-01],
[1.64041799e-07]]), array([[8.52759436e-01],
[1.52248154e-12],
[1.47240375e-01],
[1.88472871e-07]]), array([[9.92480337e-01],
[8.43223552e-13],
[7.51961814e-03],
[4.46422474e-08]]), array([[9.99428328e-01],
[4.36060531e-13],
[5.71659951e-04],
[1.17418613e-08]]), array([[9.99935140e-01],
[2.38117323e-13],
[6.48560836e-05],
[3.70506323e-09]])]
Is there any method to convert numpy.bdarray into float? Thank you for your time and your help!
Upvotes: 0
Views: 59
Reputation: 86
I think the issue here is that your output array is nested in a way that looks 3D rather than 2D, which is what the pandas DataFrame is looking for.
It looks like theta, the output of your surface_coverage function, is a matrix. While you might be interpreting it is as a list of 4 numbers (a vector), it looks like it is being treated as a matrix (1 row x 4 columns, which we can see by counting the brackets).
array([[8.53931326e-04],
[9.34890812e-15],
[9.99146054e-01],
[1.46447007e-08]])
You probably want this array to look like this (note the brackets):
array([8.53931326e-04,
9.34890812e-15,
9.99146054e-01,
1.46447007e-08])
To fix this, you should be able to replace
result.append(theta)
with
result.append(theta.flatten())
That'll turn these into just arrays. Your result will therefore just be a list of arrays which it should correctly identify as 2D.
Upvotes: 1