You can use the stargazer
package (install with pip install stargazer
).
From https://github.com/mwburke/stargazer/blob/master/examples.ipynb:
import pandas as pd
from sklearn import datasets
import statsmodels.api as sm
from stargazer.stargazer import Stargazer
from IPython.core.display import HTML
diabetes = datasets.load_diabetes()
df = pd.DataFrame(diabetes.data)
df.columns = ['Age', 'Sex', 'BMI', 'ABP', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6']
df['target'] = diabetes.target
est = sm.OLS(endog=df['target'], exog=sm.add_constant(df[df.columns[0:4]])).fit()
est2 = sm.OLS(endog=df['target'], exog=sm.add_constant(df[df.columns[0:6]])).fit()
stargazer = Stargazer([est, est2])
HTML(stargazer.render_html())
To compile it in LaTeX instead of HTML, you can use:
stargazer.render_latex()stargazer.render_latex()