pandas 格式化乳胶 (to_latex) 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15069814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Formatting latex (to_latex) output
提问by Joop
I've read about the to_latexmethod, but it's not clear how to use the formatters argument.
我已经阅读了该to_latex方法,但不清楚如何使用 formatters 参数。
I have some numbers which are too longand some which I want thousand separators.
我有一些太长的数字和一些我想要千位分隔符的数字。
A side issuefor the to_latexmethod on multi-indexed tables, the indices are parsed together and it issues some &s in the latex output.
多索引表上的方法的一个附带问题to_latex,索引被一起解析,并&在乳胶输出中发出一些s。
回答by mbatchkarov
For a simple data frame. First, without formatters:
对于一个简单的数据框。首先,没有格式化程序:
In [11]: df
Out[11]:
c1 c2
first 0.821354 0.936703
second 0.138376 0.482180
In [12]: print df.to_latex()
\begin{tabular}{|l|c|c|c|}
\hline
{} & c1 & c2 \
\hline
first & 0.821354 & 0.936703 \
second & 0.138376 & 0.482180 \
\hline
\end{tabular}
Copy-pasting the output (of [12]) to latex, we get: 
将输出(of [12])复制粘贴到乳胶,我们得到:
If we create two functions f1and f2and put them into to_latexas formatters:
如果我们创建了两个函数f1,并f2把他们变成to_latex为formatters:
def f1(x):
return 'blah_%1.2f' % x
def f2(x):
return 'f2_%1.2f' % x
In [15]: print df.to_latex(formatters=[f1, f2])
\begin{tabular}{|l|c|c|c|}
\hline
{} & c1 & c2 \
\hline
first & blah\_0.82 & f2\_0.94 \
second & blah\_0.14 & f2\_0.48 \
\hline
\end{tabular}
Copy-pasting the output to latex, we get:
将输出复制粘贴到乳胶,我们得到:
Note: how the formatter function f1is applied to the first column and f2to the second.
注意:格式化函数如何f1应用于第一列和f2第二列。

