export pandas dataframe to pdf

How to Export Pandas Dataframe to PDF

Python provides Pandas library to make it easy to work with data. It allows you to import/export data to/from files. Sometimes you may need to export Pandas dataframe to PDF. In this article, we will learn how to export Pandas Dataframe to PDF.


How to Export Pandas Dataframe to PDF

We will use matplotlib library to export Pandas dataframe to a table first and then use its functionality to export the table to PDF.


1. Import Required Modules

Create an empty python file using a text editor.

$ vi pd_to_pdf.py

Add the following lines to it.

#!/bin/python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

We basically set the execution environment and import required modules into our code.


2. Create Dataframe

Next, we create a pandas dataframe and populate it with random data, which we will convert into a table and export as PDF.

df = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3"))


3. Plot Table in Matplotlib

Next, plot the dataframe as a table in matplotlib.

fig, ax =plt.subplots(figsize=(12,4))
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=df.values,colLabels=df.columns,loc='center')


4. Export Matplotlib table to PDF

Finally, we export matplotlib table to PDF document table.pdf.

pp = PdfPages("table.pdf")
pp.savefig(fig, bbox_inches='tight')
pp.close()

In this article, we will learn how to export Pandas Dataframe to PDF. You can customize it as per your requirement.

Also read:

How to Run Python Script in Apache Web Server
Shell Script to Clear/Delete Log Files
How to Exclude Requests from Apache Log
How to Exclude Requests from NGINX Log
How to Pass Variable in cURL Command

Leave a Reply

Your email address will not be published. Required fields are marked *