Python 将熊猫中的各个列对齐到
我正在使用Python 将熊猫中的各个列对齐到,python,pandas,latex,Python,Pandas,Latex,我正在使用pandasto_latex方法将数据帧转换为latex表格格式。我看不到更改结果表格的对齐字段的选项。例如,我有一个如下所示的数据帧: In [46]: df Out[46]: Number of days Tuples Distinct Tuples 162 29 700587 41300 163 20 497599 29302 164 15 36559
pandas
to_latex
方法将数据帧转换为latex表格格式。我看不到更改结果表格的对齐字段的选项。例如,我有一个如下所示的数据帧:
In [46]: df
Out[46]:
Number of days Tuples Distinct Tuples
162 29 700587 41300
163 20 497599 29302
164 15 365599 21382
165 10 256903 14916
166 5 127647 7441
167 2 54254 3117
168 1 26987 1288
In [50]: print df.to_latex(index=None)
\begin{tabular}{lll}
\toprule
Number of days & Tuples & Distinct Tuples \\
\midrule
29 & 700587 & 41300 \\
20 & 497599 & 29302 \\
15 & 365599 & 21382 \\
10 & 256903 & 14916 \\
5 & 127647 & 7441 \\
2 & 54254 & 3117 \\
1 & 26987 & 1288 \\
\bottomrule
\end{tabular}
我的输出表如下所示:
In [46]: df
Out[46]:
Number of days Tuples Distinct Tuples
162 29 700587 41300
163 20 497599 29302
164 15 365599 21382
165 10 256903 14916
166 5 127647 7441
167 2 54254 3117
168 1 26987 1288
In [50]: print df.to_latex(index=None)
\begin{tabular}{lll}
\toprule
Number of days & Tuples & Distinct Tuples \\
\midrule
29 & 700587 & 41300 \\
20 & 497599 & 29302 \\
15 & 365599 & 21382 \\
10 & 256903 & 14916 \\
5 & 127647 & 7441 \\
2 & 54254 & 3117 \\
1 & 26987 & 1288 \\
\bottomrule
\end{tabular}
我想要的是将{lll}
对齐方式更改为{rrr}
。一般来说,我甚至可能希望不同的列有不同的对齐方式,或者甚至在{r | r | r}
指示符中使用垂直分隔符
目前是否支持此功能 正如您在熊猫的代码中所看到的(此函数用于呈现latex_表):
目前还不支持。只有当您的数据是numpy的编号时,才会将其格式化为右侧。在python中,格式化列非常容易
print df.to_latex(index=None).replace('lll','rrr')
或者以更通用的方式使用regex replace。现在(pandas
0.17.1版),to\u latex
方法具有column\u format
参数,因此您只需执行以下操作即可
print df.to_latex(index=None, column_format='rrr')