I'm enjoying learning org-babel to write literate documents and am trying to include summary tables produced by Pandas describe()
.
I found an old thread with some answers but none of the provided solutions seemed to satisfy the aim of the original poster and they seem quite clunky so I thought I'd try playing around.
Starting with the working solution I have
#+BEGIN_SRC python :exports results :results value table :return summary
import pandas as pd
import numpy as np
n = 1000
low = 0
high = 100
df = pd.DataFrame({'x': np.random.random_integers(low, high, size=n),
'y': np.random.random_integers(low, high, size=n)})
summary = df.describe()
summary = [list(summary)] + [None] + summary.values.tolist()
#+END_SRC
#+RESULTS:
| x | y |
|--------------------+--------------------|
| 1000.0 | 1000.0 |
| 49.743 | 49.326 |
| 29.186517500445365 | 29.128580435685468 |
| 0.0 | 0.0 |
| 26.0 | 24.0 |
| 49.0 | 48.0 |
| 76.0 | 75.0 |
| 100.0 | 100.0 |
And the table renders, however it has lost the index which defines what the rows are (count
, mean
, sd
, min
, 25%
, 50%
, 75%
, max
).
Pandas DataFrames have a to_html()
method so I figured that might be a viable option to return that, and it works...
#+BEGIN_SRC python :exports results :results html
import pandas as pd
import numpy as np
n = 1000
low = 0
high = 100
df = pd.DataFrame({'x': np.random.random_integers(low, high, size=n),
'y': np.random.random_integers(low, high, size=n)})
summary = df.describe()
return(summary.to_html())
#+END_SRC
#+RESULTS:
#+begin_export html
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>x</th>
<th>y</th>
</tr>
</thead>
<tbody>
<tr>
<th>count</th>
<td>1000.00000</td>
<td>1000.00000</td>
</tr>
<tr>
<th>mean</th>
<td>51.51800</td>
<td>49.76100</td>
</tr>
<tr>
<th>std</th>
<td>29.75643</td>
<td>28.97149</td>
</tr>
<tr>
<th>min</th>
<td>0.00000</td>
<td>0.00000</td>
</tr>
<tr>
<th>25%</th>
<td>26.00000</td>
<td>25.00000</td>
</tr>
<tr>
<th>50%</th>
<td>52.00000</td>
<td>48.00000</td>
</tr>
<tr>
<th>75%</th>
<td>78.00000</td>
<td>76.00000</td>
</tr>
<tr>
<th>max</th>
<td>100.00000</td>
<td>100.00000</td>
</tr>
</tbody>
</table>
#+end_export
But, what if I wanted to compile the document to LaTeX? HTML tables wouldn't be rendered correctly and I'd need to leverage the to_latex()
method instead...
#+BEGIN_SRC python :exports results :results latex
import pandas as pd
import numpy as np
n = 1000
low = 0
high = 100
df = pd.DataFrame({'x': np.random.random_integers(low, high, size=n),
'y': np.random.random_integers(low, high, size=n)})
summary = df.describe()
return(summary.to_latex())
#+END_SRC
#+RESULTS:
#+begin_export latex
\begin{tabular}{lrr}
\toprule
{} & x & y \\
\midrule
count & 1000.000000 & 1000.000000 \\
mean & 48.942000 & 50.595000 \\
std & 28.681026 & 28.868848 \\
min & 0.000000 & 0.000000 \\
25\% & 24.000000 & 25.000000 \\
50\% & 48.000000 & 50.000000 \\
75\% & 73.000000 & 76.000000 \\
max & 100.000000 & 100.000000 \\
\bottomrule
\end{tabular}
#+end_export
One of the appealing aspects, to me at least, of literate programming and org-babel is the ability to have one source file that can be output to multiple different formats on execution/compilation, but I can't work out (mainly through lack of knowledge/understanding) how to include Pandas DataFrames in a generic manner in the resulting documents.
Is it possible to have a block of code return a generic table that is rendered depending on the target output?