Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a program in Python to convert a given dataframe to a LaTex document
Converting a Pandas DataFrame to LaTeX format is useful for creating professional documents and research papers. The to_latex() method generates LaTeX table code that can be directly used in LaTeX documents.
Basic DataFrame to LaTeX Conversion
Let's start by creating a sample DataFrame and converting it to LaTeX format ?
import pandas as pd
df = pd.DataFrame({
'Id': [1, 2, 3, 4, 5],
'Age': [12, 13, 14, 15, 16]
})
print("Original DataFrame:")
print(df)
print("\nLaTeX output:")
print(df.to_latex(index=True, multirow=True))
Original DataFrame:
Id Age
0 1 12
1 2 13
2 3 14
3 4 15
4 5 16
LaTeX output:
\begin{tabular}{lrr}
\toprule
{} & Id & Age \
\midrule
0 & 1 & 12 \
1 & 2 & 13 \
2 & 3 & 14 \
3 & 4 & 15 \
4 & 5 & 16 \
\bottomrule
\end{tabular}
Customizing LaTeX Output
You can customize the LaTeX output by removing the index or changing the table format ?
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Score': [95, 87, 92],
'Grade': ['A', 'B', 'A']
})
# Without index
print("Without index:")
print(df.to_latex(index=False))
print("\n" + "="*50 + "\n")
# With custom caption and label
print("With caption and label:")
print(df.to_latex(index=False, caption="Student Scores", label="tab:scores"))
Without index:
\begin{tabular}{lrl}
\toprule
Name & Score & Grade \
\midrule
Alice & 95 & A \
Bob & 87 & B \
Charlie & 92 & A \
\bottomrule
\end{tabular}
==================================================
With caption and label:
\begin{table}
\centering
\caption{Student Scores}
\label{tab:scores}
\begin{tabular}{lrl}
\toprule
Name & Score & Grade \
\midrule
Alice & 95 & A \
Bob & 87 & B \
Charlie & 92 & A \
\bottomrule
\end{tabular}
\end{table}
Key Parameters
The to_latex() method offers several useful parameters ?
| Parameter | Description | Example Value |
|---|---|---|
index |
Include row index | True/False |
multirow |
Handle multi-level indices | True/False |
caption |
Table caption | "My Table" |
label |
LaTeX label for referencing | "tab:mytable" |
column_format |
LaTeX column alignment | "lcc" |
Saving LaTeX to File
You can save the LaTeX output directly to a file for use in your documents ?
import pandas as pd
df = pd.DataFrame({
'Product': ['Laptop', 'Mouse', 'Keyboard'],
'Price': [999.99, 25.50, 75.00],
'Stock': [50, 200, 100]
})
# Save to file
with open('table.tex', 'w') as f:
f.write(df.to_latex(index=False, caption="Product Inventory"))
print("LaTeX table saved to 'table.tex'")
print("\nGenerated LaTeX:")
print(df.to_latex(index=False, caption="Product Inventory"))
LaTeX table saved to 'table.tex'
Generated LaTeX:
\begin{table}
\centering
\caption{Product Inventory}
\begin{tabular}{lrr}
\toprule
Product & Price & Stock \
\midrule
Laptop & 999.99 & 50 \
Mouse & 25.50 & 200 \
Keyboard & 75.00 & 100 \
\bottomrule
\end{tabular}
\end{table}
Conclusion
Use df.to_latex() to convert DataFrames into professional LaTeX tables. Set index=False to hide row indices and add caption and label parameters for complete table environments. This method is essential for integrating data analysis results into academic papers and reports.
