Converting statsmodels summary object to Pandas Dataframe Converting statsmodels summary object to Pandas Dataframe pandas pandas

Converting statsmodels summary object to Pandas Dataframe


The answer from @Michael B works well, but requires "recreating" the table. The table itself is actually directly available from the summary().tables attribute. Each table in this attribute (which is a list of tables) is a SimpleTable, which has methods for outputting different formats. We can then read any of those formats back as a pd.DataFrame:

import statsmodels.api as smmodel = sm.OLS(y,x)results = model.fit()results_summary = results.summary()# Note that tables is a list. The table at index 1 is the "core" table. Additionally, read_html puts dfs in a list, so we want index 0results_as_html = results_summary.tables[1].as_html()pd.read_html(results_as_html, header=0, index_col=0)[0]


Store your model fit as a variable results, like so:

import statsmodels.api as smmodel = sm.OLS(y,x)results = model.fit()

Then create a a function like below:

def results_summary_to_dataframe(results):    '''take the result of an statsmodel results table and transforms it into a dataframe'''    pvals = results.pvalues    coeff = results.params    conf_lower = results.conf_int()[0]    conf_higher = results.conf_int()[1]    results_df = pd.DataFrame({"pvals":pvals,                               "coeff":coeff,                               "conf_lower":conf_lower,                               "conf_higher":conf_higher                                })    #Reordering...    results_df = results_df[["coeff","pvals","conf_lower","conf_higher"]]    return results_df

You can further explore all the attributes of the results object by using dir() to print, then add them to the function and df accordingly.


An easy solution is just one line of code:

LRresult = (result.summary2().tables[1])

As ZaxR mentioned in the following comment, Summary2 is not yet considered stable, while it works well with Summary too. So this could be correct answer:

LRresult = (result.summary().tables[1])

This will give you a dataframe object:

type(LRresult)

pandas.core.frame.DataFrame

To get the significant variables and run the test again:

newlist = list(LRresult[LRresult['P>|z|']<=0.05].index)[1:]myform1 = 'binary_Target' + ' ~ ' + ' + '.join(newlist)M1_test2 = smf.logit(formula=myform1,data=myM1_1)result2 = M1_test2.fit(maxiter=200)LRresult2 = (result2.summary2().tables[1])LRresult2