As of writing, this is only possible with my fork of pandas, but hopefully it will make its way into the main pandas stable branch.
The purpose of the fork is to let you specify a custom html formatter for individual columns in a data frame.
In this example we create a formatting function which takes a numpy array and returns a string of the form <img src='--base64-encoded-data'/>
. This means that the numpy array is displayed as an image.
Below is the code we use to define our custom format function. Note that the function takes a single element from the data frame and returns and html string::
import numpy as np
from io import BytesIO
from PIL import Image
from base64 import b64encode
from matplotlib import cm
from matplotlib.colors import Normalize
def matshow_func(x):
b = BytesIO()
norm = Normalize(clip=True)
if not np.ma.isMaskedArray(x):
x = np.ma.array(x,mask = np.isnan(x))
c = cm.jet(norm(x), bytes=True)
Image.fromarray(c).save(b, format='png')
return '<img alt="2d array" src="data:image/png;base64,' + b64encode(b.getvalue()) + '" />'
matshow_func.escape = False # This prevents the "<" tags getting escaped
matshow_func.justify = 'all' # This prevents the long string of data getting abrieviated with "..."
Now lets create an example data frame:
import pandas as pd
df = pd.DataFrame(columns=('boring','interesting'),index=arange(4))
df.interesting[0] = np.arange(225).reshape((15,15))
df.interesting[2] = np.arange(225).reshape((15,15)).T
df.interesting[3] = np.random.randint(100,size=(15,15))
df.boring[2] = np.arange(225).reshape((15,15))
Right, now whenever we type df.to_html(formatters=dict(interesting=matshow_func))
on the IPython prompt, we see:
{pandas_screenshot.png - see attachment below}
You may want to wrap your df
in another class which stores the dict of formatters and implementes _repr_html_
, passing the dict to df.to_html
. Attached below are some sample function factories for creating slightly more customized formatters.
Thank you for sharing! Exactly the function I am searching for!