Home > Mobile >  Print whole numbers as integers in Pandas LaTeX conversion
Print whole numbers as integers in Pandas LaTeX conversion

Time:02-06

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?

CodePudding user response:

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}

CodePudding user response:

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)
  • Related