csv 在Pandas LaTeX转换中将整数打印为整数

ufj5ltwl  于 2023-02-06  发布在  其他
关注(0)|答案(2)|浏览(263)

我正在尝试写一个小脚本来打印基于CSV的LaTeX表格。以前在matrix2latex中的很多功能现在都包含在Pandas中了,这很酷。
然而,无论我做什么(我在这里尝试了许多其他的建议,结果变成了一个错综复杂的烂摊子,实际上什么也改变不了),它总是这样出现:

[deco]/tmp/table ❱ python lala.py

Dataframe:

  Unnamed: 0     Treatment Implant Transgenic Untreated Mice  Transgenic Treated Mice  Wildtype Mice  Total Mice
0         P1   Armodafinil     VTA                     20+15                     20.0            5.0          60
1         P2           NaN      LC                        50                      NaN           10.0          60
2         P3  Escitalopram      DR                        20                     20.0            NaN          40
3         P4    Reboxetine      LC                        20                     20.0            NaN          40

LaTeX Table Conversion:

\begin{tabular}{lllllrrr}
 & Unnamed: 0 & Treatment & Implant & Transgenic Untreated Mice & Transgenic Treated Mice & Wildtype Mice & Total Mice \\
0 & P1 & Armodafinil & VTA & 20+15 & 20.000000 & 5.000000 & 60 \\
1 & P2 & nan & LC & 50 & nan & 10.000000 & 60 \\
2 & P3 & Escitalopram & DR & 20 & 20.000000 & nan & 40 \\
3 & P4 & Reboxetine & LC & 20 & 20.000000 & nan & 40 \\
\end{tabular}

[deco]/tmp/table ❱ cat lala.py
import pandas as pd

df = pd.read_csv("table.csv")
print("\n")
print("Dataframe:")
print("")
print(df)
tex = df.style.to_latex()
print("\n")
print("LaTeX Table Conversion:")
print("")
print(tex)
[deco]/tmp/table ❱ cat table.csv
,Treatment,Implant,Transgenic Untreated Mice,Transgenic Treated Mice,Wildtype Mice,Total Mice
P1,Armodafinil,VTA,20+15,20,5,60
P2,N/A,LC,50,,10,60
P3,Escitalopram,DR,20,20,,40
P4,Reboxetine,LC,20,20,,40

有没有办法确保整数总是显示为整数?

kmb7vmvb

kmb7vmvb1#

您似乎面临的问题是,缺少的表项会被解释为NaN,这会强制整个列浮动。您可以防止空项被读为NaN,并通过使用以下命令使它们保持为空

import pandas as pd

df = pd.read_csv("table.csv", keep_default_na=False)
print(df)
tex = df.style.to_latex()
print(tex)

这导致以下输出:

Unnamed: 0     Treatment Implant Transgenic Untreated Mice Transgenic Treated Mice Wildtype Mice  Total Mice
0         P1   Armodafinil     VTA                     20+15                      20             5          60
1         P2           N/A      LC                        50                                    10          60
2         P3  Escitalopram      DR                        20                      20                        40
3         P4    Reboxetine      LC                        20                      20                        40
\begin{tabular}{lllllllr}
 & Unnamed: 0 & Treatment & Implant & Transgenic Untreated Mice & Transgenic Treated Mice & Wildtype Mice & Total Mice \\
0 & P1 & Armodafinil & VTA & 20+15 & 20 & 5 & 60 \\
1 & P2 & N/A & LC & 50 &  & 10 & 60 \\
2 & P3 & Escitalopram & DR & 20 & 20 &  & 40 \\
3 & P4 & Reboxetine & LC & 20 & 20 &  & 40 \\
\end{tabular}

如果你对行的编号不满意,你可以去掉csv文件第一行的第一个逗号,得到如下的输出:

Treatment Implant Transgenic Untreated Mice Transgenic Treated Mice Wildtype Mice  Total Mice
P1   Armodafinil     VTA                     20+15                      20             5          60
P2           N/A      LC                        50                                    10          60
P3  Escitalopram      DR                        20                      20                        40
P4    Reboxetine      LC                        20                      20                        40
\begin{tabular}{llllllr}
 & Treatment & Implant & Transgenic Untreated Mice & Transgenic Treated Mice & Wildtype Mice & Total Mice \\
P1 & Armodafinil & VTA & 20+15 & 20 & 5 & 60 \\
P2 & N/A & LC & 50 &  & 10 & 60 \\
P3 & Escitalopram & DR & 20 & 20 &  & 40 \\
P4 & Reboxetine & LC & 20 & 20 &  & 40 \\
\end{tabular}
mklgxw1f

mklgxw1f2#

在转换为LaTeX之前,可以使用.astype(int)函数设置整数的格式。此外,还可以使用format选项控制LaTeX中所有列的格式:

import pandas as pd

df = pd.read_csv("table.csv")

# Format columns as integers
df = df.astype({"Transgenic Untreated Mice": int, "Transgenic Treated Mice": int, "Wildtype Mice": int, "Total Mice": int})

# Format all columns as integers in LaTeX
tex = df.style.format("{:.0f}").to_latex()

print("\n")
print("Dataframe:")
print("")
print(df)
print("\n")
print("LaTeX Table Conversion:")
print("")
print(tex)

相关问题