LaTeX for Data Science: Tables From CSV & pandas
Generate LaTeX tables from data automatically: use pandas df.to_latex() or the csvsimple package to import a CSV directly, so your tables update with your data.
To generate LaTeX tables from data, export them programmatically with pandas df.to_latex() or import a CSV directly with the csvsimple package — so your tables update automatically when the data changes. Hand-typing a 40-row results table is slow and error-prone; generating it from the source data keeps your paper and your analysis in sync. Here are both workflows.
1. From pandas (Python)
import pandas as pd
df = pd.read_csv("results.csv")
# Basic LaTeX tabular
print(df.to_latex(index=False, column_format="lrr", float_format="%.2f"))
# Or write straight to a file you \input in LaTeX
with open("results/table1.tex", "w") as f:
f.write(df.to_latex(index=False, float_format="%.2f"))
Then in your document:
\begin{table}[t]
\centering
\caption{Experimental results.}
\input{results/table1}
\end{table}
Rerun the analysis and the table refreshes on the next compile.
2. Import a CSV directly
The csvsimple package reads a CSV at compile time — no Python step:
\usepackage{csvsimple}
\usepackage{booktabs}
...
\csvautobooktabular{data.csv}
\csvautobooktabular builds a booktabs-styled table automatically. For full control over formatting per column, use \csvreader. This keeps the CSV as the single source of truth.
3. Which approach to choose
| Situation | Use |
|---|---|
| Data from a Python pipeline | df.to_latex() → \input |
| Data in a shared spreadsheet/CSV | csvsimple direct import |
| Need exact publication styling | pandas + manual booktabs |
| Large numeric tables | pgfplotstable |
4. Make it publication-grade
Whichever route you choose, finish with booktabs rules and siunitx decimal alignment — see the full tables guide. For charts from the same data, plot with pgfplots so the figure matches your fonts — see TikZ diagrams.
5. The reproducible-paper pattern
Have your analysis script write both the tables and the figures into a results/ folder that your document \inputs — the data-science version of a multi-file project. The paper then can never disagree with the data.
→ Drop your generated .tex tables into LetX and see them typeset instantly.
Written by Shihab Shahriar Antor — AI Engineer & Founder of Shahriar Labs, maker of LetX.
Frequently Asked Questions
How do I convert a pandas DataFrame to a LaTeX table?
Use df.to_latex() in Python, which returns a tabular environment as a string. Pass options like index=False to drop the index, column_format='lrr' to set alignment, and float_format='%.2f' to fix decimals. For publication-quality output, add the booktabs styling by writing the result with \toprule and \midrule, or use the styler API df.style.to_latex(). Save the string to a .tex file and \input it so the table refreshes whenever you rerun the analysis.
Can I import a CSV into LaTeX without converting it first?
Yes, with the csvsimple or pgfplotstable package. \csvautotabular{data.csv} reads the file and builds a table automatically, while \csvreader gives row-by-row control over formatting. This keeps the CSV as the single source of truth — edit the data file and the table updates on the next compile, with no Python step. It is ideal when your data changes often or lives in a shared spreadsheet.
What's the best workflow to keep tables in sync with changing data?
Generate the table file from your analysis and \input it. In a reproducible pipeline, your script writes results/table1.tex with df.to_latex(), and your document has \input{results/table1}. Rerunning the analysis regenerates the table, and the next compile picks it up — so the paper never disagrees with the data. This input-generated-file pattern is the data-science equivalent of a multi-file project.