Preparation
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.
- How to install Pandas on PyCharm
- How to install Tabulate on PyCharm
- How to install Tables on PyCharm
- How to install lxml on PyCharm
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
DataFrame.to_latex()
The to_latex()
method converts a DataFrame into a formatted LaTeX document. This document can then save as a separate file.
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)
Parameter | Description |
---|---|
buf | This parameter is the file path/buffer to write. If empty, a string returns. |
columns | This parameter is the sub-set of columns to write. If empty, all columns write. |
col_space | This depicts the length of each column. |
header | This parameter writes out the column names. |
index | This parameter writes out the row (index) names. |
na_rep | This parameter represents the string value for missing data. |
formatters | This parameter is a formatter function to apply to elements by position/name. |
float_format | This parameter is a formatter for floating-point numbers. |
sparsify | If True and MultiIndex, display the key for each row. |
index_names | This parameter displays the index names. |
bold_rows | This parameter displays the row names in bold. |
column_format | This parameter is the column format as outlined in the LaTeX table format |
longtable | The value of this parameter is read from the pandas config module. If True , use a longtable format instead of tabular. |
escape | The value of this parameter is read from the pandas config module. If False , prevent escaping LaTeX special characters in column names. |
encoding | A string representation of encoding. By default, UTF-8 is used. |
decimal | This parameter is the decimal separator, comma (, ) in Europe. |
multicolumn | If True , use multi-column to enhance MultiIndex columns. |
multicolumn_format | This parameter is the alignment for multi-columns. |
multirow | If True , use multi-row to enhance MultiIndex rows. |
caption | This parameter is a tuple containing the caption. |
label | This parameter is the LaTeX label inside \label{} in the output. |
position | This 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))), columns=cols) print(df) 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
).
Output
df
Name | Username | Fav Color | |
0 | Pete | 1998_pete2 | (139, 0, 139) |
1 | Leon | Gar_man | (143, 188, 143) |
2 | Isla | Isla2021 | (173, 216, 230) |
3 | Blake | kirbster | (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.