Categories
Python Answers

How to save a pandas DataFrame table as a png with Python?

Spread the love

Sometimes, we want to save a pandas DataFrame table as a png with Python.

In this article, we’ll look at how to save a pandas DataFrame table as a png with Python.

How to save a pandas DataFrame table as a png with Python?

To save a pandas DataFrame table as a png with Python, we can use the data frame export method in the dataframe_image package.

To install it, we run

pip install dataframe_image

Then we write

import pandas as pd
import numpy as np
import dataframe_image as dfi

df = pd.DataFrame(np.random.randn(6, 6), columns=list('ABCDEF'))
df_styled = df.style.background_gradient()
dfi.export(df_styled, "mytable.png")

to create a 6 column data frame with some random numbers with

df = pd.DataFrame(np.random.randn(6, 6), columns=list('ABCDEF'))

Then we call df.style.background_gradient to style the data frame table with a background gradient.

Finally, we call dfi.export with the df_styled data frame and the file name to save the table image to.

Conclusion

To save a pandas DataFrame table as a png with Python, we can use the data frame export method in the dataframe_image package.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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