簡體   English   中英

對齊pandas to_latex中的各列

[英]Aligning individual columns in pandas to_latex

我正在使用pandas to_latex方法將數據幀轉換為乳膠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}指示符中。

目前是否支持?

到目前為止( pandas版本0.17.1), to_latex方法有column_format參數,所以你可以簡單地做

print df.to_latex(index=None, column_format='rrr')

正如你在pandas代碼中看到的那樣(這個函數用於渲染latex_table): https//github.com/pydata/pandas/blob/master/pandas/core/format.py#L492目前還不支持。 只有當您的數據是numpy的數字時,它才會被格式化為正確。 在python中,很容易格式化列。

print df.to_latex(index=None).replace('lll','rrr')

或者以更通用的方式使用正則表達式替換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM