0

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]]
2
  • it almost seems like your output is a list of dataframes Commented Jun 8, 2020 at 15:22
  • Yes it is. figured out the problem :) Commented Jun 10, 2020 at 8:13

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.