用 Python 編寫一個程式,將給定的資料框轉換為一個 LaTex 文件
假設你有一個數據框,結果轉換為 LaTeX 如下所示,
\begin{tabular}{lrr}
\toprule
{} & Id & Age \
\midrule
0 & 1 & 12 \
1 & 2 & 13 \
2 & 3 & 14 \
3 & 4 & 15 \
4 & 5 & 16 \
\bottomrule
\end{tabular}解決方案
為了解決這個問題,我們將遵循以下步驟 −
定義一個數據框
對資料框應用 to_latex() 函式並將 index 和多行值設定為 True。如下所示,
df.to_latex(index = True, multirow = True)
示例
讓我們檢視下列程式碼以更好地理解 −
import pandas as pd
df = pd.DataFrame({'Id': [1,2,3,4,5],
'Age': [12,13,14,15,16]})
print(df.to_latex(index = True, multirow = True))輸出
\begin{tabular}{lrr}
\toprule
{} & Id & Age \
\midrule
0 & 1 & 12 \
1 & 2 & 13 \
2 & 3 & 14 \
3 & 4 & 15 \
4 & 5 & 16 \
\bottomrule
\end{tabular}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP