
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - LaTeX
The Pandas library allows exporting DataFrame, Series, and Styler objects into LaTeX tabular representations, enabling easy integration of tabular data into LaTeX documents.
LaTeX is a high-quality typesetting system used for creating professional documents, particularly in scientific and technical fields. It can handle complex mathematical formulas, and scientific symbols. The LaTeX tabular representation arranges the content in rows and columns, by using its defined structure. Each cell in the table is stored with text or data, and rows are separated by line breaks.
Below is an example of a simple LaTeX tabular representation, where rows are separated by the "\\" and columns by "&" −
\begin{tabular}{lrr} & Col1 & Col2 \\ Row1 & 1 & 2 \\ Row2 & 3 & 4 \\ \end{tabular}
In this tutorial, we will learn how to export Pandas objects to LaTeX using the to_latex() method, including advanced customization options provided by the Styler object.
Key Considerations for Working with LaTeX in Pandas
Here are some important points to keep in mind when working with LaTeX in Pandas −
Styler Integration: The DataFrame.to_latex() method now internally uses the Styler.to_latex() implementation, which offers greater flexibility and advanced formatting capabilities.
Installation Requirements: The jinja2 library is required to use the LaTeX export functionality in Pandas v2.0.0 and later.
Export-Only Functionality: Currently, Pandas only supports exporting data to LaTeX. It does not provide the ability to read LaTeX files.
Writing Pandas Objects to LaTeX
You can export Pandas DataFrame, Series, or Styler object to a LaTex using the to_latex() method. This method converts Pandas data into LaTeX tables, supporting various formats like tabular, longtable, and nested tables.
Example
This example shows how to convert a Pandas DataFrames to LaTex tabular representations using the DataFrame.to_latex() method.
import pandas as pd # Create a simple DataFrame df = pd.DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["c", "d"]) # Display the Input DataFrame print("Original DataFrame:") print(df) # Export to LaTeX using Styler latex_output = df.style.to_latex() print("\nGenerated LaTeX Code:") print(latex_output)
When we run above program, it produces following result −
Original DataFrame:
c | d | |
---|---|---|
a | 1 | 2 |
b | 3 | 4 |
Formatting Values Before Exporting to LaTeX
You can format table values before exporting using either the formatters parameter of the to_latex() method or the Styler.format() method.
Example: Formatting Values using the to_latex() Method
This example demonstrates formatting a DataFrame values before exporting to LaTeX table using the DataFrame.to_latex() methods and the formatters parameter.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col_1": range(3), "Col_2": ['a', 'b', 'c']}) print("Original DataFrame:") print(df) # Convert DataFrame to LaTeX format latex_output = df.to_latex(index=False, formatters={"Col_1": float, "Col_2": str.upper}) print("\nFormatted LaTeX Output:") print(latex_output)
Following is an output of the above code −
Original DataFrame:
Col1 | Col2 | |
---|---|---|
r1 | 0 | a |
r2 | 1 | b |
r3 | 2 | c |
Example: Formatting Values using the Styler.format() Method
You can format the values in a DataFrame before exporting using the Styler.format() method. This is especially useful for displaying currency, percentages, or custom string formatting.
This example uses the Styler.format() method to format values before LaTeX output of a Pandas DataFrame.
import pandas as pd # Create a simple DataFrame df = pd.DataFrame({"Col_1": range(3), "Col_2": ['a', 'b', 'c']}, index=['r1', 'r2', 'r3']) # Display the Input DataFrame print("Original DataFrame:") print(df) # Format values as currency before LaTeX export latex_output = df.style.format({"Col_1": "{}", "Col_2": str.upper}).to_latex() print("\nFormatted LaTeX Output:") print(latex_output)
While executing the above code we get the following output −
Original DataFrame:
Col1 | Col2 | |
---|---|---|
r1 | 0 | a |
r2 | 1 | b |
r3 | 2 | c |
Exporting Hierarchical Indexed Object to LaTeX
Pandas supports exporting hierarchical indexed objects (multi-row and multi-column indices) to LaTeX, making it suitable for complex datasets.
Example
This example shows how to export a MultiIndex DataFrame to LaTeX using the multicolumn and multirow parameters of the .to_latex() method.
import pandas as pd import numpy as np # Create hierarchical indexing for rows and columns row_index = pd.MultiIndex.from_arrays( [["BMW", "BMW", "Lexus", "Lexus", "Audi", "Audi"], ["1", "2", "1", "2", "1", "2"]], names=["Brand", "Model"] ) column_index = pd.MultiIndex.from_arrays( [["Performance", "Performance", "Price", "Price"], ["Speed", "Mileage", "USD", "Discount"]], names=["Attribute", "Details"] ) # Create a MultiIndex DataFrame data = np.random.rand(6, 4) df = pd.DataFrame(data, index=row_index, columns=column_index) print("Original MultiIndexed DataFrame:") print(df) # Convert DataFrame to LaTeX with multirow and multicolumn options latex_output = df.to_latex(multicolumn=True, multirow=True, ) print("\nMultiIndex LaTeX Output:") print(latex_output)
When we run above program, it produces following result −
Original MultiIndexed DataFrame:
Attribute | Performance | Price | |||
---|---|---|---|---|---|
Details | Speed | Mileage | USD | Discount | |
Brand | Model | ||||
BMW | 1 | 0.346620 | 0.315143 | 0.213465 | 0.444698 |
2 | 0.588380 | 0.378466 | 0.709013 | 0.836503 | |
Lexus | 1 | 0.586889 | 0.343801 | 0.202830 | 0.737780 |
2 | 0.559294 | 0.896489 | 0.501153 | 0.440781 | |
Audi | 1 | 0.228265 | 0.654666 | 0.606819 | 0.598872 |
2 | 0.520143 | 0.447575 | 0.088034 | 0.140516 |