英文:
Print whole numbers as integers in Pandas LaTeX conversion
问题
有没有办法确保整数始终显示为整数?
翻译好的部分:
"Is there any way to make sure that whole numbers are always displayed as integers?"
英文:
I'm trying to write a small script to print LaTeX tables based on CSV.
A lot of the functionality formerly in e.g. matrix2latex
have now been included in Pandas proper, which is cool.
However, no matter what I do (I tried a number of the other suggestions on here, it ended up becoming a convoluted mess which in effect changes nothing), it keeps coming out like this:
[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
Is there any way to make sure that whole numbers are always displayed as integers?
答案1
得分: 1
你似乎遇到的问题是缺失的表格条目被解释为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}
英文:
the issue, that you seem to be facing is that missing table entries are interpreted as NaN, which then forces the entire column to float you can prevent the empty entries from getting read as NaN and have them just left empty by using
import pandas as pd
df = pd.read_csv("table.csv", keep_default_na=False)
print(df)
tex = df.style.to_latex()
print(tex)
this leads to the following output:
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}
if you're dissatisfied with the numbering of the rows you might want to remove the first comma on the first line of your csv to get an output that looks like this:
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}
答案2
得分: 0
你可以在转换为LaTeX之前使用.astype(int)
函数格式化整数。此外,你可以使用格式选项来控制LaTeX中所有列的格式:
import pandas as pd
df = pd.read_csv("table.csv")
# 格式化列为整数
df = df.astype({"Transgenic Untreated Mice": int, "Transgenic Treated Mice": int, "Wildtype Mice": int, "Total Mice": int})
# 在LaTeX中将所有列格式化为整数
tex = df.style.format("{:.0f}").to_latex()
print("\n")
print("数据框:")
print("")
print(df)
print("\n")
print("LaTeX表格转换:")
print("")
print(tex)
英文:
You can format the integers using the .astype(int)
function before converting to LaTeX. Also, you can use the format option to control the formatting of all columns in 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)
答案3
得分: 0
以下是翻译好的部分:
"A more general way to do that would be to convert the missing values to an integer (if possible) and to convert the datatype using convert_dtypes."
更一般的做法是将缺失值转换为整数(如果可能的话),并使用 convert_dtypes 转换数据类型。
"The same works without replacing nan values, convert_dtypes
then replaces the datatype to Int64."
不替换NaN值的情况下也适用,convert_dtypes
然后将数据类型替换为Int64。
import pandas as pd
df = pd.read_csv("table.csv")
print("\n")
print("Dataframe:")
print("")
print(df)
tex = df.fillna(-1).convert_dtypes().to_latex()
print("\n")
print("LaTeX Table Conversion:")
print("")
print(tex)
LaTeX 表格转换:
\begin{tabular}{lllllrrr}
\toprule
{} & Unnamed: 0 & Treatment & Implant & Transgenic Untreated Mice & Transgenic Treated Mice & Wildtype Mice & Total Mice \\
\midrule
0 & P1 & Armodafinil & VTA & 20+15 & 20 & 5 & 60 \\
1 & P2 & -1 & LC & 50 & -1 & 10 & 60 \\
2 & P3 & Escitalopram & DR & 20 & 20 & -1 & 40 \\
3 & P4 & Reboxetine & LC & 20 & 20 & -1 & 40 \\
\bottomrule
\end{tabular}
英文:
A more general way to do that would be to convert the missing values to an integer (if possible) and to convert the datatype using convert_dtypes
.
The same works without replacing nan values, convert_dtypes
then replaces the datatype to Int64.
import pandas as pd
df = pd.read_csv("table.csv")
print("\n")
print("Dataframe:")
print("")
print(df)
tex = df.fillna(-1).convert_dtypes().to_latex()
print("\n")
print("LaTeX Table Conversion:")
print("")
print(tex)
LaTeX Table Conversion:
\begin{tabular}{lllllrrr}
\toprule
{} & Unnamed: 0 & Treatment & Implant & Transgenic Untreated Mice & Transgenic Treated Mice & Wildtype Mice & Total Mice \\
\midrule
0 & P1 & Armodafinil & VTA & 20+15 & 20 & 5 & 60 \\
1 & P2 & -1 & LC & 50 & -1 & 10 & 60 \\
2 & P3 & Escitalopram & DR & 20 & 20 & -1 & 40 \\
3 & P4 & Reboxetine & LC & 20 & 20 & -1 & 40 \\
\bottomrule
\end{tabular}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论