从 Pandas DataFrame 获取乳胶字符串时如何删除索引列

Gab*_*ris 8 python latex pandas

to_latex最初,您可以简单地在Pandas 对象的方法中传递一个参数DataFrame。现在您会收到有关签名更改的警告消息。例子:

>>> import pandas as pd

>>> import numpy as np

>>> data = {f'Column {i + 1}': np.random.randint(0, 10, size=(10, )) for i in range(5)}

>>> df = pd.DataFrame(data)

>>> df
   Column 1  Column 2  Column 3  Column 4  Column 5
0         1         8         3         3         5
1         8         6         7         7         3
2         2         1         6         1         1
3         9         7         9         5         5
4         5         4         7         8         9
5         9         5         3         6         2
6         6         9         9         6         8
7         8         7         2         6         5
8         4         9         4         6         2
9         2         6         5         3         0

>>> lat_og = df.to_latex(index=False)
<ipython-input-7-986346043a05>:1: FutureWarning: In future versions `DataFrame.to_latex` is expected to utilise the base implementation of `Styler.to_latex` for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use `DataFrame.style.to_latex` which also contains additional functionality.
  lat_og = df.to_latex(index=False)

>>> print(lat_og)
\begin{tabular}{rrrrr}
\toprule
 Column 1 &  Column 2 &  Column 3 &  Column 4 &  Column 5 \\
\midrule
        1 &         8 &         3 &         3 &         5 \\
        8 &         6 &         7 &         7 &         3 \\
        2 &         1 &         6 &         1 &         1 \\
        9 &         7 &         9 &         5 &         5 \\
        5 &         4 &         7 &         8 &         9 \\
        9 &         5 &         3 &         6 &         2 \\
        6 &         9 &         9 &         6 &         8 \\
        8 &         7 &         2 &         6 &         5 \\
        4 &         9 &         4 &         6 &         2 \\
        2 &         6 &         5 &         3 &         0 \\
\bottomrule
\end{tabular}
Run Code Online (Sandbox Code Playgroud)

import warnings您可以获得没有索引列的所需输出,但如果它会发生变化,或者我必须不断修复它,我不想继续使用它。

警告消息建议我们使用该style属性。如何使用style属性来忽略索引列?to_latex我阅读了与该属性关联的方法的文档style,但它没有上面的简单参数。例子:

>>> lat_new = df.style.to_latex(hrules=True)

>>> print(lat_new)
\begin{tabular}{lrrrrr}
\toprule
 & Column 1 & Column 2 & Column 3 & Column 4 & Column 5 \\
\midrule
0 & 1 & 8 & 3 & 3 & 5 \\
1 & 8 & 6 & 7 & 7 & 3 \\
2 & 2 & 1 & 6 & 1 & 1 \\
3 & 9 & 7 & 9 & 5 & 5 \\
4 & 5 & 4 & 7 & 8 & 9 \\
5 & 9 & 5 & 3 & 6 & 2 \\
6 & 6 & 9 & 9 & 6 & 8 \\
7 & 8 & 7 & 2 & 6 & 5 \\
8 & 4 & 9 & 4 & 6 & 2 \\
9 & 2 & 6 & 5 & 3 & 0 \\
\bottomrule
\end{tabular}
Run Code Online (Sandbox Code Playgroud)

索引列位于 LaTeX 中。有哪些方法可以在不使用原始方法的情况下删除索引列?

编辑

如果您遇到此问题,请注意 pandas 开发人员正计划弃用该DataFrame.to_latex()方法。他们目前正在使用该Styler.to_latex()方法。目前的签名并不相同,需要额外的方法来隐藏索引列或转义乳胶语法。有关该过程的更多最新更新,请参阅41649,有关兔子洞的开始,请参阅44411 。他们计划在 pandas 2.0 中修复这个问题。

小智 8

It is possible to use the hide() method of the style attribute of a Pandas dataframe. The following code will produce a LaTeX table without the values of the index:

import pandas as pd
import numpy as np

data = {f'Column {i + 1}': np.random.randint(0, 10, size=(10, )) for i in range(5)}
df = pd.DataFrame(data)
lat_new = df.style.hide(axis="index").to_latex(hrules=True)
print(lat_new)
Run Code Online (Sandbox Code Playgroud)

The result is the following:

\begin{tabular}{rrrrr}
\toprule
Column 1 & Column 2 & Column 3 & Column 4 & Column 5 \\
\midrule
2 & 5 & 9 & 2 & 2 \\
2 & 6 & 0 & 9 & 5 \\
3 & 2 & 4 & 3 & 2 \\
8 & 9 & 3 & 7 & 8 \\
5 & 9 & 7 & 4 & 4 \\
0 & 3 & 2 & 2 & 6 \\
5 & 7 & 7 & 8 & 6 \\
2 & 2 & 9 & 3 & 3 \\
6 & 0 & 0 & 9 & 2 \\
4 & 8 & 7 & 5 & 9 \\
\bottomrule
\end{tabular}
Run Code Online (Sandbox Code Playgroud)