'how to transform the results of a looped OLS into a dataframe?
I'm trying to turn the results of a looped regression into a dataframe, in a way that I can quickly skim trough and figure out which models are relevant. The problem is that my code is returning individual dataframes for each of the columns in the loop. Do you know how I can turn this into one single df output? Here is a df example you can use to try the code.
questions=['despise_it','involved_it','authority_it','cross_legal','cross_difficult',
'cross_covid', 'cross_future', 'wildlife_crime', 'wildlife_interested', 'wildlife_involved', 'wildlife_affected', 'wildlife_plastics']
## empty model
models = []
##regressions
for y in questions:
models = sm.OLS(laos[y],laos['new_income'],missing='drop')
result = models.fit()
results_df = pd.DataFrame({"variable": result.model.endog_names,"pvals":result.pvalues,"coeff":result.params})
print(results_df)
The output I'm getting looks like this:

Any help will be highly appreciated!
Solution 1:[1]
Before running the model create an empty dataframe
df = pd.DataFrame(columns = ["variable","pvals","coeff"])
then after each iterations create a row and append it to the Dataframe.
new_row = {"variable":result.model.endog_names, "pvals":result.pvalues,"coeff":result.params}
then append each row to the Dataframe before it goes to the next iteration.
df = df.append(new_row,ignore_index = True)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vivek Sthanam |
