
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a program in Python to convert a given dataframe to a LaTex document
Assume, you have a dataframe and the result for converted to latex as,
\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}
Solution
To solve this, we will follow the steps given below −
Define a dataframe
Apply to_latex() function to the dataframe and set index and multirow values as True. It is defined below,
df.to_latex(index = True, multirow = True)
Example
Let’s check the following code to get a better understanding −
import pandas as pd df = pd.DataFrame({'Id': [1,2,3,4,5], 'Age': [12,13,14,15,16]}) print(df.to_latex(index = True, multirow = True))
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}
- Related Articles
- Write a Python program to reshape a given dataframe in different ways
- Write a program in Python to localize Asian timezone for a given dataframe
- Write a Python code to combine two given series and convert it to a dataframe
- Write a program in Python to remove first duplicate rows in a given dataframe
- Write a program in Python Pandas to convert a dataframe Celsius data column into Fahrenheit
- Write a program in Python to transpose the index and columns in a given DataFrame
- Write a Python program to sort a given DataFrame by name column in descending order
- Write a program in Python to modify the diagonal of a given DataFrame by 1
- Write a program in Python to select any random odd index rows in a given DataFrame
- Write a Python program to separate a series of alphabets and digits and convert them to a dataframe
- Write a Python code to rename the given axis in a dataframe
- Write a Python code to filter palindrome names in a given dataframe
- Write a program in Python to count the total number of leap years in a given DataFrame
- Write a program in Python to count the records based on the designation in a given DataFrame
- Write a program in Python to remove one or more than one columns in a given DataFrame

Advertisements