Reputation: 11
I'm fairly new to python and I'm facing an issue with the datatype returned by a function. I have two functions prepare and model
def Prepare(Inputdf):
x0 = list(range(e ,s+1 ,-1))
X = Inputdf[x0]
Y = Inputdf['y0']
return [X,Y]
# Linear Model
def Model(Inputdf, X, Y):
#Train
X = sm.add_constant(X)
lmodel = sm.OLS(Y, X).fit()
summ = lmodel.summary()
#Test
x1 = list(range(e-1 ,s ,-1))
A = Inputdf[x1]
A = sm.add_constant(A)
newcol = lmodel.predict(A)
newdf = pd.concat([Inputdf, newcol], axis=1)
return [newdf]
They are called in the loop below. The function returns a list but I'm not sure how to convert the output into a dataframe
forecast = []
for i in wh:
TrainFinal = TrainInput[TrainInput['column'].isin([i])]
TestFinal = TestInput[TestInput['column'].isin([i])]
Modelresult = Model(TestFinal, *Prepare(TrainFinal))
forecast.append(Modelresult)
print (forecast)
the output looks like this
[[ country y1 0
6990 United Arab Emirates 42.0 24.314137
... ... ... ...
7426 United Arab Emirates 12.0 8.281969
[71 rows x 3 columns]],
[ country y1 0
8338 United Arab Emirates 97.0 66.740899
... ... ... ...
8696 United Arab Emirates 8.0 12.884740
[67 rows x 3 columns]],
[ country y1 0
284 Chile 6.0 16.672239
... ... ... ...
374 Chile 5.0 13.534690
[67 rows x 3 columns]]
Upvotes: 0
Views: 69
Reputation: 11
I found out what I was doing wrong. I just removed the brackets in front of the returned dataframe in the 'Model Function'
return [newdf] -> return newdf
then I concatenated the results instead of appending them, and I reset the index to get my final output :)
forecast = pd.concat(Modelresult)
forecast.reset_index(drop=True, inplace=True)
Upvotes: 1