Python

Pandas to LaTeX

If you are new to the Python programming language, you may not be familiar with the user-friendly behavior of the language. Python is not just user-friendly but extremely popular and can be used for many general purposes. However, the python programming language has not been developed for statistical analysis or graphic designing; with time, it has immensely been used in analyzing data and experimenting and computing statistics. Hence it offers various types of libraries for almost every use case.

Pandas is one of python’s most popular and general-purpose libraries that is used as a manipulation tool and data analysis. Using Pandas, we can perform various functions and can export, load, or save different formats, which include, LaTeX, Excel, CSV, etc. In this article, we will discuss how to get the DataFrame in the form of a latex document.

What is a LaTeX?

LaTeX is a high-quality typesetting and document preparation system. It is a method of creating documents using plain text and styled by markup tags similar to CSS or HTML. It is normally used to design scientific, technical documents, and academic documents like journal or conference papers.

However, it is also used for several other types of publishing. It allows developers or authors, or writers to provide basic information in a structured format, for example, title, the name of the author, publishing date, abstract, detailed text, etc. It is free software available for all operating systems, including Linux, Windows, Mac OS, etc.

What is a LaTeX Document?

A LaTeX document is a Word-like plain text document used for communication and publication of scientific, technical, and academic documents. You can easily develop and design complex text design and formatting in LaTex which is quite difficult or probably impossible in a simple Word document. For example, we need to write the following equation in a proper format:

Creating this equation in Word is a pain; however, it is very simple in LaTeX; you just need to write the equation in a plain text format, and LaTeX software will create the formatted equation for you. Here is the syntax of the above equation in LaTeX:

\sum_{0}^{n}
\frac{5\alpha  + \beta -9\gamma}
{\int \partial  b-a^5}

It just takes a couple of minutes and a few lines to display a complex equation nicely in LaTex. Moreover, LaTeX can manage more than just an equation like lists, references, bibliographies, tables of contents, formulaic drawings, and, more specifically, non-roman alphabets. Hence when writing in LaTeX, you do not have to worry about typesetting. The built-in capabilities of LaTeX can manage it all.

How to Get Pandas to LaTeX?

Now we know what LaTeX is and what is a LaTeX document. Now, let us move forward and see how we can get a LaTeX document to or from a python code. As we have discussed earlier, python is a highly user-friendly language, and its libraries are capable of doing almost everything. Converting, exporting, or importing a file in LaTeX format is one of those features.

Pandas provide a to_latex() function that can get the DataFrame in the form of a LaTeX document. Below you can follow the example to learn how to get the DataFrame in the form of a LaTeX document.

Example 1

In this example, we will simply create a DataFrame which consists of the student names and their score in a math subject. After that, we will use the to_latex() function to the output as a LaTeX document. Now, let’s see the code below. Here the first line shows that we have imported the pandas’ library and created the dataframe afterward. Finally, the latex() function is used to show it as a latex document.

import pandas as pd
Marks = pd.DataFrame({'Student Name': ['Math Score'],
                    'Mathew': ['52'],
                    'Ribs': ['98'],
                    'Shawn':['79'],
                    'Danial':['92']})
 
print(Marks.to_latex(index = True, multirow = True))

Below is the output of the above code, which is in the LaTeX format. Observe that each line is presented in a complete formatted way of LaTeX. Developing a LaTeX document is very easy; however, python makes it easier to develop a LaTeX document as you do not have to remember multiple lines of code but just one word, and that is to_latex().

Example 2

Let us demonstrate another example; here, we will add another column in the previously defined DataFrame. First, we have just defined student names and math scores; now, we will add the gender of the student in the DataFrame as well. Below is the code to add another column in the DataFrame:

import pandas as pd
Marks = pd.DataFrame({'Student Name': ['Math Score', 'Gender'],
                    'Mathew': ['52', 'Male'],
                    'Ribs': ['98', 'Female'],
                    'Shawn':['79', 'Male'],
                    'Danial':['92','Female']})
 
print(Marks.to_latex(index = True, multirow = True))

Here is the output of the above code. Note that another line is added to the LaTeX document with an added index.

Example 3

In this example, we will make you learn another way of presenting the same information in a different way. In previous examples, we have demonstrated the data column-wise. Now, we will demonstrate the input information in a DataFrame in a row-wise format. The code is given below to show the data in a row format.

import pandas as pd
df = pd.DataFrame(dict(SName=['Mathew', 'Ribs', 'Shawn', 'Danial'],
                  Color=['red', 'White', 'purple', 'blue'],
                  marks=['50', '90', '80', '100']))
print(df.to_latex(index=False))

Let us see the output below:

Conclusion

In this article, we have discussed what LaTeX is and what is a LaTeX document. We have discussed how we can get the LaTeX document from pandas in python. The Pandas in python provide the to_latex() function to get the input DataFrame in a LaTeX document. The to_latex() function takes the DataFrame as input in different formats and converts the input data into the LaTeX document. With the help of simple examples, we have demonstrated how to use the to_latex() function in the python code and easily convert the python DataFrame to a LaTeX document.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content