<p>You can use the <a href="https://github.com/mwburke/stargazer" rel="noreferrer"><code>stargazer</code> package</a> (install with <code>pip install stargazer</code>).</p>

<p>From <a href="https://github.com/mwburke/stargazer/blob/master/examples.ipynb" rel="noreferrer">https://github.com/mwburke/stargazer/blob/master/examples.ipynb</a>:</p>

<pre><code>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())
</code></pre>

<p><a href="https://i.sstatic.net/57jK1.png" rel="noreferrer"><img src="https://i.sstatic.net/57jK1.png" alt="enter image description here"></a></p>

<p>To compile it in LaTeX instead of HTML, you can use:
<code>stargazer.render_latex()</code></p>

CC BY-SA 4.0
2020-02-09T03:28:35.797Z
2020-06-12T22:04:29.647Z
2020-06-12T22:04:29.647Z
Max Ghenis
<p>There is now a <a href="https://github.com/mwburke/stargazer/" rel="noreferrer">Python version</a> of the well known <strong>stargazer</strong> R package, which does exactly this.</p>

<p>See also this related question: <a href="https://stackoverflow.com/q/35051673/2858145">https://stackoverflow.com/q/35051673/2858145</a></p>

CC BY-SA 4.0
2019-10-12T14:52:13.847Z
2019-10-12T14:52:13.847Z
Pietro Battiston
<p>You can use code like the following (making use of the as_latex function) to output a regression result to a tex file but it doesn't stack them neatly in tabular form the way that <a href="https://ideas.repec.org/c/boc/bocode/s456416.html" rel="noreferrer">outreg2</a> does:</p>

<pre><code>import pandas as pd
import statsmodels.formula.api as smf
x = [1, 3, 5, 6, 8, 3, 4, 5, 1, 3, 5, 6, 8, 3, 4, 5, 0, 1, 0, 1, 1, 4, 5, 7]
y = [0, 1, 0, 1, 1, 4, 5, 7,0, 1, 0, 1, 1, 4, 5, 7,0, 1, 0, 1, 1, 4, 5, 7]
d = { "x": pd.Series(x), "y": pd.Series(y)}
df = pd.DataFrame(d)
mod = smf.ols('y ~ x', data=df)
res = mod.fit()
print(res.summary())

beginningtex = """\\documentclass{report}
\\usepackage{booktabs}
\\begin{document}"""
endtex = "\end{document}"

f = open('myreg.tex', 'w')
f.write(beginningtex)
f.write(res.summary().as_latex())
f.write(endtex)
f.close()
</code></pre>

<p>The as_latex function makes a valid latex table but not a valid latex document, so I added some additional code above so that it would compile. The result is something like this for the print function:</p>

<pre><code>                        OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.129
Model:                            OLS   Adj. R-squared:                  0.089
Method:                 Least Squares   F-statistic:                     3.257
Date:                Fri, 29 Apr 2016   Prob (F-statistic):             0.0848
Time:                        20:12:12   Log-Likelihood:                -53.868
No. Observations:                  24   AIC:                             111.7
Df Residuals:                      22   BIC:                             114.1
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P&gt;|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept      0.9909      0.908      1.091      0.287        -0.893     2.875
x              0.3732      0.207      1.805      0.085        -0.056     0.802
==============================================================================
Omnibus:                        3.957   Durbin-Watson:                   0.999
Prob(Omnibus):                  0.138   Jarque-Bera (JB):                1.902
Skew:                           0.380   Prob(JB):                        0.386
Kurtosis:                       1.849   Cond. No.                         8.50
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
</code></pre>

<p>and like this for the latex:
<a href="https://i.sstatic.net/dz4Ap.png" rel="noreferrer"><img src="https://i.sstatic.net/dz4Ap.png" alt="enter image description here"></a></p>

<p>Update:
Not as full featured as outreg but the summary_col function does what you ask. </p>

<pre><code>import pandas as pd
import statsmodels.formula.api as smf
from statsmodels.iolib.summary2 import summary_col
x = [1, 3, 5, 6, 8, 3, 4, 5, 1, 3, 5, 6, 8, 3, 4, 5, 0, 1, 0, 1, 1, 4, 5, 7]
y = [0, 1, 0, 1, 1, 4, 5, 7,0, 1, 0, 1, 1, 4, 5, 7,0, 1, 0, 1, 1, 4, 5, 7]
d = { "x": pd.Series(x), "y": pd.Series(y)}
df = pd.DataFrame(d)
df['xsqr'] = df['x']**2  
mod = smf.ols('y ~ x', data=df)
res = mod.fit()
print(res.summary())
df['xcube'] = df['x']**3  

mod2= smf.ols('y ~ x + xsqr', data=df)
res2 = mod2.fit()
print(res2.summary())

mod3= smf.ols('y ~ x + xsqr + xcube', data=df)
res3 = mod3.fit()
print(res2.summary())

dfoutput = summary_col([res,res2,res3],stars=True)
print(dfoutput)
</code></pre>

<p>Which has the following output:</p>

<pre><code>=====================================
            y I       y II    y III  
-------------------------------------
Intercept 0.9909   -0.6576   -0.2904 
          (0.9083) (1.0816)  (1.3643)
x         0.3732*  1.7776*** 1.0700  
          (0.2068) (0.6236)  (1.6736)
xcube                        -0.0184 
                             (0.0402)
xsqr               -0.1845** 0.0409  
                   (0.0781)  (0.4995)
=====================================
Standard errors in parentheses.
* p&lt;.1, ** p&lt;.05, ***p&lt;.01
</code></pre>

<p>As before, you can use the dfoutput.as_latex() to export this to latex. </p>

CC BY-SA 3.0
2016-05-01T18:56:28.777Z
Thanks for the detailed response!

Unfortunately, I know how to do this, because this is the output of a single regression.

I am trying to display a table with the outputs of multiple regressions, like here:

http://statadaily.ikonomiya.com/wp-content/uploads/2010/10/outreg_sample_result1.jpg

I will update my question to clarify this!
Rajko Radovanovic
CC BY-SA 4.0
2019-01-24T13:08:41.610Z
Do you know where this functions is documented? I cannot find it on the statsmodels website search. Thanks!
Guilherme Salome
CC BY-SA 3.0
2016-04-30T00:16:46.733Z
2016-05-02T19:55:09.557Z
2016-05-02T19:55:09.557Z
BKay
<p>Anyone know of a way to get multiple regression outputs (not multivariate regression, literally multiple regressions) in a table indicating which different independent variables were used and what the coefficients / standard errors were, etc. </p>

<p>Essentially, I'm looking for something like outreg, except for python and statsmodels. This seems promising but I don't understand how it works:</p>

<p><a href="http://statsmodels.sourceforge.net/stable/generated/statsmodels.iolib.summary.Summary.html#statsmodels.iolib.summary.Summary" rel="noreferrer">http://statsmodels.sourceforge.net/stable/generated/statsmodels.iolib.summary.Summary.html#statsmodels.iolib.summary.Summary</a></p>

<p>To be fully clear here, I wondering how to make tables similar to the example below:</p>

<p><a href="https://i.sstatic.net/vxKCD.jpg" rel="noreferrer"><img src="https://i.sstatic.net/vxKCD.jpg" alt="enter image description here"></a></p>

CC BY-SA 3.0
2016-04-29T19:25:00.440Z
p.s. this thread might be better suited on the data analysis platforms, not sure...
Rajko Radovanovic
CC BY-SA 4.0
2019-10-15T04:26:49.657Z
I'm voting to close this question as off-topic because it is about Python programming/command and not about economics.
Art
<regression><stata><python>
Outputting Regressions as Table in Python (similar to outreg in stata)?
Rajko Radovanovic
# Outputting Regressions as Table in Python (similar to outreg in stata)?

(Asked by: username_0 on 2016-04-29)

Anyone know of a way to get multiple regression outputs (not multivariate regression, literally multiple regressions) in a table indicating which different independent variables were used and what the coefficients / standard errors were, etc.

Essentially, I'm looking for something like outreg, except for python and statsmodels. This seems promising but I don't understand how it works:

<http://statsmodels.sourceforge.net/stable/generated/statsmodels.iolib.summary.Summary.html#statsmodels.iolib.summary.Summary>

To be fully clear here, I wondering how to make tables similar to the example below:

[![enter image description here](https://i.sstatic.net/vxKCD.jpg)](https://i.sstatic.net/vxKCD.jpg)

**Tags:** regression, stata, python
> **Comments:**
> **username_0**: p.s. this thread might be better suited on the data analysis platforms, not sure...  
> **username_1**: I'm voting to close this question as off-topic because it is about Python programming/command and not about economics.  

---

## 3 answers

### Answer by username_4 on 2016-04-30. Score: 12 | Accepted answer

You can use code like the following (making use of the as_latex function) to output a regression result to a tex file but it doesn't stack them neatly in tabular form the way that outreg2 does:
import pandas as pd
import statsmodels.formula.api as smf
x = [1, 3, 5, 6, 8, 3, 4, 5, 1, 3, 5, 6, 8, 3, 4, 5, 0, 1, 0, 1, 1, 4, 5, 7]
y = [0, 1, 0, 1, 1, 4, 5, 7,0, 1, 0, 1, 1, 4, 5, 7,0, 1, 0, 1, 1, 4, 5, 7]
d = { "x": pd.Series(x), "y": pd.Series(y)}
df = pd.DataFrame(d)
mod = smf.ols('y ~ x', data=df)
res = mod.fit()
print(res.summary())

beginningtex = """\\documentclass{report}
\\usepackage{booktabs}
\\begin{document}"""
endtex = "\end{document}"

f = open('myreg.tex', 'w')
f.write(beginningtex)
f.write(res.summary().as_latex())
f.write(endtex)
f.close()

The as_latex function makes a valid latex table but not a valid latex document, so I added some additional code above so that it would compile. The result is something like this for the print function:
                        OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.129
Model:                            OLS   Adj. R-squared:                  0.089
Method:                 Least Squares   F-statistic:                     3.257
Date:                Fri, 29 Apr 2016   Prob (F-statistic):             0.0848
Time:                        20:12:12   Log-Likelihood:                -53.868
No. Observations:                  24   AIC:                             111.7
Df Residuals:                      22   BIC:                             114.1
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept      0.9909      0.908      1.091      0.287        -0.893     2.875
x              0.3732      0.207      1.805      0.085        -0.056     0.802
==============================================================================
Omnibus:                        3.957   Durbin-Watson:                   0.999
Prob(Omnibus):                  0.138   Jarque-Bera (JB):                1.902
Skew:                           0.380   Prob(JB):                        0.386
Kurtosis:                       1.849   Cond. No.                         8.50
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

and like this for the latex:

Update:
Not as full featured as outreg but the summary_col function does what you ask. 
import pandas as pd
import statsmodels.formula.api as smf
from statsmodels.iolib.summary2 import summary_col
x = [1, 3, 5, 6, 8, 3, 4, 5, 1, 3, 5, 6, 8, 3, 4, 5, 0, 1, 0, 1, 1, 4, 5, 7]
y = [0, 1, 0, 1, 1, 4, 5, 7,0, 1, 0, 1, 1, 4, 5, 7,0, 1, 0, 1, 1, 4, 5, 7]
d = { "x": pd.Series(x), "y": pd.Series(y)}
df = pd.DataFrame(d)
df['xsqr'] = df['x']**2  
mod = smf.ols('y ~ x', data=df)
res = mod.fit()
print(res.summary())
df['xcube'] = df['x']**3  

mod2= smf.ols('y ~ x + xsqr', data=df)
res2 = mod2.fit()
print(res2.summary())

mod3= smf.ols('y ~ x + xsqr + xcube', data=df)
res3 = mod3.fit()
print(res2.summary())

dfoutput = summary_col([res,res2,res3],stars=True)
print(dfoutput)

Which has the following output:
=====================================
            y I       y II    y III  
-------------------------------------
Intercept 0.9909   -0.6576   -0.2904 
          (0.9083) (1.0816)  (1.3643)
x         0.3732*  1.7776*** 1.0700  
          (0.2068) (0.6236)  (1.6736)
xcube                        -0.0184 
                             (0.0402)
xsqr               -0.1845** 0.0409  
                   (0.0781)  (0.4995)
=====================================
Standard errors in parentheses.
* p<.1, ** p<.05, ***p<.01

As before, you can use the dfoutput.as_latex() to export this to latex.

> **Comments:**
> **username_0**: Thanks for the detailed response!  Unfortunately, I know how to do this, because this is the output of a single regression.  I am trying to display a table with the outputs of multiple regressions, like here:  http://statadaily.ikonomiya.com/wp-content/uploads/2010/10/outreg_sample_result1.jpg  I will update my question to clarify this!  
> **username_5**: Do you know where this functions is documented? I cannot find it on the statsmodels website search. Thanks!  

---

### Answer by username_2 on 2020-02-09. Score: 7

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()


---

### Answer by username_3 on 2019-10-12. Score: 5

There is now a [Python version](https://github.com/mwburke/stargazer/) of the well known **stargazer** R package, which does exactly this.

See also this related question: <https://stackoverflow.com/q/35051673/2858145>


---

