How to save pandas dataframe into existing pdf from PdfPages

Viewed 7929

I have created a pdf that saves several plots created using Matplotlib.

I did the following to create the pdf

from matplotlib.backends.backend_pdf import PdfPages
report = PdfPages('report.pdf')

After creating a plot, I would do this report.savefig() each time. However, I also want to output dataframes I generated into the Pdf. Essentially I want a report contain plots and queried dataframes all in one place. Is it possible to add a dataframe to the Pdf using the one created with PdfPages and if so, how would I do so? If not, is there another approach that would allow the plots and dataframe to be in once place (without having to save individual components and piecing them together)? Would love any suggestions and examples. Thanks!

1 Answers

Just create a plot of the table, then save that. Given a dataframe such as:

import pandas as pd

df = pd.DataFrame()
df['Animal'] = ['Cow', 'Bear']
df['Weight'] = [250, 450]
df['Favorite'] = ['Grass', 'Honey']
df['Least Favorite'] = ['Meat', 'Leaves']

which looks like:

  Animal  Weight Favorite Least Favorite
0    Cow     250    Grass           Meat
1   Bear     450    Honey         Leaves

you can plot a table version of it like so:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(9,2))
ax = plt.subplot(111)
ax.axis('off')
ax.table(cellText=df.values, colLabels=df.columns, bbox=[0,0,1,1])

Output:

enter image description here

You can style the table plot a little nicer by adding some background color to the cells:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(9,2))
ax=plt.subplot(111)
ax.axis('off')
c = df.shape[1]
ax.table(cellText=np.vstack([df.columns, df.values]), cellColours=[['lightgray']*c] + [['none']*c]*2, bbox=[0,0,1,1])

Output:

enter image description here

See this ongoing thread (from which all these examples were taken) for more ideas/variants.

Edit

It occurred to me that you might want to plot images and tables on the same figure. You can do so to get results like this:

enter image description here

Here's a link to the tutorial that image came from, which has some example code to help get you started.

Related