我的具体问题是,很多时候我在 org-mode 中使用 python 源块对数据帧的许多变量运行相同的统计分析,然后将它们呈现为乳胶表。手动进行每个分析变得非常乏味,所以我想使用 for 循环:
#+begin_src python :exports results :session :results latex
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 3, 4, 5],
'b': [9, 8, 7, 6, 5]})
for var in df.columns:
df[var].value_counts().to_latex()
#+end_src
问题是它只返回源块返回的最后一个值(for循环的最后一次运行。
#+Results:
#+BEGIN_LaTeX
\begin{tabular}{lr}
\toprule
{} & b \\
\midrule
7 & 1 \\
6 & 1 \\
5 & 1 \\
9 & 1 \\
8 & 1 \\
\bottomrule
\end{tabular}
#+END_LaTeX
那么有没有办法从一个源代码块中实际获取多个乳胶或组织表?
编辑:考虑@dschwilk 的答案,我需要返回多个#+Results 块(每个latex 或org 表一个),以便我可以在它们之间添加文本描述。如:
Description for table 1
#+RESULTS:
#+BEGIN_LaTeX
\begin{tabular}{lr}
\toprule
{} & a \\
\midrule
5 & 1 \\
4 & 1 \\
3 & 1 \\
2 & 1 \\
1 & 1 \\
\bottomrule
\end{tabular}
#+END_LaTeX
Description for table 2
#+RESULTS:
\begin{tabular}{lr}
\toprule
{} & b \\
\midrule
7 & 1 \\
6 & 1 \\
5 & 1 \\
9 & 1 \\
8 & 1 \\
\bottomrule
\end{tabular}
#+END_LaTeX