Reputation: 17924
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?
Upvotes: 0
Views: 221
Reputation: 333
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}
Upvotes: 0
Reputation: 11
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}
Upvotes: 1
Reputation: 1
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)
Upvotes: 0