Pandas DataFrame to_latex() Method

Rate this post


Before any data manipulation can occur, four (4) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The Tabulate library enables formatted output.
  • The Tables library allows formatted output (table format).
  • The lxml library enables writing to an XML file.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install tabulate

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install tables

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install lxml

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.

Feel free to view the PyCharm installation guide for the required libraries.

Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import tabulate
import tables
import lxml


The to_latex() method converts a DataFrame into a formatted LaTeX document. This document can then save as a separate file.

Pandas DataFrame to LaTeX table - LaTeX course

The syntax for this method is as follows:

DataFrame.to_latex(buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, bold_rows=False, column_format=None, longtable=None, escape=None, encoding=None, decimal='.', multicolumn=None, multicolumn_format=None, multirow=None, caption=None, label=None, position=None)
bufThis parameter is the file path/buffer to write. If empty, a string returns.
columnsThis parameter is the sub-set of columns to write.
If empty, all columns write.
col_spaceThis depicts the length of each column.
headerThis parameter writes out the column names.
indexThis parameter writes out the row (index) names.
na_repThis parameter represents the string value for missing data.
formattersThis parameter is a formatter function to apply to elements by position/name.
float_formatThis parameter is a formatter for floating-point numbers.
sparsifyIf True and MultiIndex, display the key for each row.
index_namesThis parameter displays the index names.
bold_rowsThis parameter displays the row names in bold.
column_formatThis parameter is the column format as outlined in the LaTeX table format 
longtableThe value of this parameter is read from the pandas config module. If True, use a longtable format instead of tabular.
escapeThe value of this parameter is read from the pandas config module. If False, prevent escaping LaTeX special characters in column names.
encodingA string representation of encoding. By default, UTF-8 is used.
decimalThis parameter is the decimal separator, comma (,) in Europe.
multicolumnIf True, use multi-column to enhance MultiIndex columns.
multicolumn_formatThis parameter is the alignment for multi-columns.
multirowIf True, use multi-row to enhance MultiIndex rows.
captionThis parameter is a tuple containing the caption.
labelThis parameter is the LaTeX label inside \label{} in the output.
positionThis parameter is the table position. This option is placed after \begin{} in the output.

The owner of Finxters has decided to add some additional details for their users: their favorite color in RGB format. At present, this is just a test.

Run the code to create a LaTeX document (.tex) file containing sample users.

cols = ('Name', 'Username', 'Fav Color'); 
df = pd.DataFrame ((('Pete',  '1998_pete2', (139, 0, 139)),
                    ('Leon',  'Gar_man',    (143, 188, 143)),
                    ('Isla',  'Isla2021',   (173, 216, 230)),
                    ('Blake', 'kirbster',   (147, 112, 219))), 

df.to_latex('finxters.tex', index=False, caption='User Details')
  • Line [1] creates column names and saves them to the cols list.
  • Line [2] creates a DataFrame with user data and sets the columns to the cols variable created above.
  • Line [3] outputs the DataFrame to the terminal.
  • Line [4] saves the DataFrame to a LaTeX file (finxters.tex).



NameUsernameFav Color
0Pete1998_pete2(139, 0, 139)
1LeonGar_man(143, 188, 143)
2IslaIsla2021(173, 216, 230)
3Blakekirbster(147, 112, 219)

finxters.tex file

💡 Note: You can also use an online converter to convert a tex file to a pdf.

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.