用 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