How to Save a Pandas Dataframe Table as a Png

How to save a pandas DataFrame table as a png

Pandas allows you to plot tables using matplotlib (details here).
Usually this plots the table directly onto a plot (with axes and everything) which is not what you want. However, these can be removed first:

import matplotlib.pyplot as plt
import pandas as pd
from pandas.table.plotting import table # EDIT: see deprecation warnings below

ax = plt.subplot(111, frame_on=False) # no visible frame
ax.xaxis.set_visible(False) # hide the x axis
ax.yaxis.set_visible(False) # hide the y axis

table(ax, df) # where df is your data frame


The output might not be the prettiest but you can find additional arguments for the table() function here.
Also thanks to this post for info on how to remove axes in matplotlib.


Here is a (admittedly quite hacky) way of simulating multi-indexes when plotting using the method above. If you have a multi-index data frame called df that looks like:

first  second
bar one 1.991802
two 0.403415
baz one -1.024986
two -0.522366
foo one 0.350297
two -0.444106
qux one -0.472536
two 0.999393
dtype: float64

First reset the indexes so they become normal columns

df = df.reset_index() 
first second 0
0 bar one 1.991802
1 bar two 0.403415
2 baz one -1.024986
3 baz two -0.522366
4 foo one 0.350297
5 foo two -0.444106
6 qux one -0.472536
7 qux two 0.999393

Remove all duplicates from the higher order multi-index columns by setting them to an empty string (in my example I only have duplicate indexes in "first"):

df.ix[df.duplicated('first') , 'first'] = '' # see deprecation warnings below
first second 0
0 bar one 1.991802
1 two 0.403415
2 baz one -1.024986
3 two -0.522366
4 foo one 0.350297
5 two -0.444106
6 qux one -0.472536
7 two 0.999393

Change the column names over your "indexes" to the empty string

new_cols = df.columns.values
new_cols[:2] = '','' # since my index columns are the two left-most on the table
df.columns = new_cols

Now call the table function but set all the row labels in the table to the empty string (this makes sure the actual indexes of your plot are not displayed):

table(ax, df, rowLabels=['']*df.shape[0], loc='center')

et voila:

enter image description here

Your not-so-pretty but totally functional multi-indexed table.


As pointed out in the comments, the import statement for table:

from import table

is now deprecated in newer versions of pandas in favour of:

from pandas.plotting import table 

The ix indexer has now been fully deprecated so we should use the loc indexer instead. Replace:

df.ix[df.duplicated('first') , 'first'] = ''


df.loc[df.duplicated('first') , 'first'] = ''

Can I save a table/dataframe to a file (like png/jpg) in python?

Assuming this table is a pandas DataFrame, this library might help:

This library would export pandas DataFrames in a jupyter notebook fashioned way.

Example usage:

import pandas as pd
import dataframe_image as dfi
df = pd.DataFrame({'key':[1,2,3],'val':['a','b','c']})
dfi.export(df, 'dataframe.png')

enter image description here

Save pandas table (filled with strings) as png

First of all your columns lists have to be the same length. You may plot table with matplotlib and table function from pandas.

import pandas as pd
import matplotlib.pylab as plt
from import table

# I add None value to align all lists
column1 = ['Measured Set', '1. set', '2. set', '3. set']
column2= ['Breached parameter (number of breaches, %)', None, None,None ]
column3 = ['Breached parameter (number of breaches, %)', None, None,None]

data = {
'Sensor': column1,
'Sensor 1': column2,
'Sensor 2': column3,

df = pd.DataFrame(data)

# set fig size
fig, ax = plt.subplots(figsize=(12, 3))
# no axes
# no frame
# plot table
tab = table(ax, df, loc='upper right')
# set font manually
# save the result

enter image description here

How to save the Pandas dataframe/series data as a figure?

Option-1: use matplotlib table functionality, with some additional styling:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame()
df['date'] = ['2016-04-01', '2016-04-02', '2016-04-03']
df['calories'] = [2200, 2100, 1500]
df['sleep hours'] = [8, 7.5, 8.2]
df['gym'] = [True, False, False]

def render_mpl_table(data, col_width=3.0, row_height=0.625, font_size=14,
header_color='#40466e', row_colors=['#f1f1f2', 'w'], edge_color='w',
bbox=[0, 0, 1, 1], header_columns=0,
ax=None, **kwargs):
if ax is None:
size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
fig, ax = plt.subplots(figsize=size)
mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, **kwargs)

for k, cell in mpl_table._cells.items():
if k[0] == 0 or k[1] < header_columns:
cell.set_text_props(weight='bold', color='w')
cell.set_facecolor(row_colors[k[0]%len(row_colors) ])
return ax.get_figure(), ax

fig,ax = render_mpl_table(df, header_columns=0, col_width=2.0)

enter image description here

Options-2 Use Plotly + kaleido

import plotly.figure_factory as ff
import pandas as pd

df = pd.DataFrame()
df['date'] = ['2016-04-01', '2016-04-02', '2016-04-03']
df['calories'] = [2200, 2100, 1500]
df['sleep hours'] = [8, 7.5, 8.2]
df['gym'] = [True, False, False]

fig = ff.create_table(df)
fig.write_image("table_plotly.png", scale=2)

enter image description here

For the above, the font size can be changed using the font attribute:


How I can save big tables as image with pandas

I realize you mentioned image, but I would look into trying to make it into an html page. Luckily, pandas can handle all of it for us with the df.to_html() method. Definitely take a look at the documentation which allows lots of nifty options (html classes, ids, borders, etc etc).

But as an example, it would look something like this:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(1538, 4),
columns=['a', 'b', 'c', 'd'])
with open('dataframe.html', 'w') as outfile:

Then you can just open up dataframe.html in any browser.

Related Topics

Leave a reply