Data Engineering Neo4j Pandas

Neo4j + Pandas = Inline Image

Sometimes, we experience image URL in Neo4j data, and as a Data Engineer / Data Scientist, we would like to see the image. The query result set in Neo4j doesn’t has the capabilities to display.

Lets assume, we have a dataset, containing the URL of images from as an attribute.

Jupyter notebook, CSV and loader script can be found at Github

My choice of python library to interact with Neo4j is py2neo. As always, pandas is most popular library for any data engineer / data scientist. To display inline images in Pandas dataframe, we need to import HTML method from IPython.display module.

import pandas as pd
from py2neo.database import Graph
import requests
import sys
from IPython.display import HTML

Connect to the graph database using

# initialize Graph context
graph = Graph('bolt://<ip_address>:7687', auth=('neo4j', '<password>'), name="<db_name>")

Create a dataframe

books = pd.DataFrame("match (b:book) return b.isbn,b.title,b.yop,b.url").to_table(),columns=['isbn','title','yop','url']) 


A small code-snippet function is created to get the relative path of the URL.

def path_to_image_html(path):
    return '<img src="'+ path + '"  >'

Now, we will utilized the HTML method from IPython to display the dataframe with images.

pd.set_option('display.max_colwidth', None)
display(HTML(books.to_html(escape=False ,formatters=dict(url=path_to_image_html))))

The resulting dataframe with images directly grabbed from the internet, not from local storage.

 127 total views,  2 views today

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.