簡體   English   中英

格式化代碼 to_latex 來自 Python Pandas

[英]Formatting code to_latex from Python Pandas

我正在嘗試將我在 pandas 上創建的 dataframe 轉換為 Overleaf 上的 LatEx 文件。 為此,我使用以下代碼從 Python 檢索了適用的代碼:

print(macro_df.to_latex())

我收到了以下 output:

\begin{tabular}{lr}
\toprule
{} &      0 \\
macro\_ind                         &        \\
\midrule
Agriculture, forestry and fishing &    293 \\
Construction                      &    947 \\
Manufacturing                     &  36425 \\
Mining                            &   4239 \\
Retail Trade                      &   4594 \\
Services                          &  14221 \\
Transportation                    &   5187 \\
Wholesale trade                   &   2310 \\
\bottomrule
\end{tabular}

一旦我在 Overleaf 提示中復制粘貼,值和文本都非常好,唯一的問題是表格太薄。 我想手動選擇表格在頁面中所占的寬度,但我一直無法這樣做。 我嘗試過使用tabularxtabular*包,但它們不能在 Overleaf 上使用(出於某種原因 - 也許我做錯了什么?)所以我使用的是tabular 我也嘗試過格式化列寬\begin{tabular}{l{8cm}r{2m}} ,但它似乎不起作用。

有人可以幫助我嗎,因為我花了幾個小時嘗試和嘗試!

預先感謝您的幫助:)

將表從 pandas 移動到 latex 的任何其他可行解決方案也將受到歡迎!

您是否嘗試過\hspace作為解決方法? 像這樣的東西:

...
\toprule
‎\hspace{10cm}‎ &      0 \\
...

您可以像這樣定義自己的列類型:

\newcolumntype{L}{>{\raggedright\arraybackslash}X}

\begin{table}[!h]

\begin{tabularx}{\textwidth}{L{nncm}L{nncm}
blah & blah \\
\end{tabularx}

\end{table}

這也會很好地換行。 nn是您希望該列有多大或將其留空並讓 textwidth 為您處理

暫無
暫無

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

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