Reputation: 275
I have the following code, which takes each column of the data frame HF and fit it to the entire dataframe Index. What I am interested then is to produce a dataframe Betas which contains the regression coefficients:
col_names = HF.columns
Betas = pd.DataFrame(columns= HF.columns, index = Index.columns)
for name in col_names:
lm = linear_model.LinearRegression()
hf = HF[name]
model = lm.fit(Index, hf)
Betas.loc[:][name] = lm.coef_
the code runs but what I get is a Betas dataframe with only NaN values. Can you please help understand the problem? Thanks
Upvotes: 0
Views: 1086
Reputation: 9257
The problem is that after .loc
you are slicing the Betas
dataframe twice, whereas you need to do it only once by using the same set of squared brackets.
This should be your new for-cycle
for name in col_names:
lm = linear_model.LinearRegression()
hf = HF[name]
lm.fit(Index, hf)
Betas.loc[:, name] = lm.coef_
As a sidenote, I removed the assignment to model
as it is not necessary; you can just call the fit
method on the lm
object.
Upvotes: 1