You need to install below package to run this example.

pip install py2neo


In [4]:
from py2neo import Graph, Node, Relationship

In [62]:
## Connecting to graph database and creating a graph of cities
## I am using default user `neo4j` and password is changed to `admin`
g = Graph("http://neo4j:admin@localhost:7474")

In [63]:
type(g)


Out[63]:
py2neo.database.Graph

In [11]:
## Starting a transaction on graph and adding nodes to it
tx = g.begin()
with open('node_city.txt') as f_in:
    for line in f_in:
        city_list = [x.strip() for x in line.rstrip().split(',')]
        city = Node("City", name=city_list[0], population=int(city_list[1]))
        tx.create(city)
tx.commit()

In [76]:
## Running a command on Graph
for node in g.run("MATCH (a:City) RETURN a.name, a.population"):
    print(node)


<Record a.name='New_York' a.population=8491079>
<Record a.name='Los_Angeles' a.population=3928864>
<Record a.name='Chicago' a.population=2722389>
<Record a.name='Houston' a.population=2239558>
<Record a.name='Philadelphia' a.population=1560297>
<Record a.name='Phoenix' a.population=1537058>
<Record a.name='San_Antonio' a.population=1436697>
<Record a.name='San_Diego' a.population=1381069>
<Record a.name='Dallas' a.population=1281047>
<Record a.name='San_Jose' a.population=1015785>
<Record a.name='Austin' a.population=912791>
<Record a.name='Jacksonville' a.population=853382>
<Record a.name='San_Francisco' a.population=852469>
<Record a.name='Indianapolis' a.population=848788>
<Record a.name='Columbus' a.population=835957>
<Record a.name='Fort_Worth' a.population=812238>
<Record a.name='Charlotte' a.population=809958>
<Record a.name='Detroit' a.population=680250>
<Record a.name='Seattle' a.population=66342>
<Record a.name='Denver' a.population=663862>

In [95]:
##Select nodes of this graph
nodes = g.nodes

In [99]:
## Making relationship between nodes
tx = g.begin()
with open('edge_distance.txt') as f_in:
    for line in f_in:
        edge_list = [x.strip() for x in line.split(',')]
        city1_name = edge_list[0]
        city2_name = edge_list[1]
        city1_node = nodes.match(name=city1_name)
        city2_node = nodes.match(name=city2_name)
        city_pair = Relationship(city1_node.first(), 'FLIGHT_BETWEEN', city2_node.first())
        city_pair['distance'] = edge_list[2]
        tx.create(city_pair)
tx.commit()

In [104]:
##importing pandas for analysis
import pandas as pd

In [112]:
## Importing relationship matcher
from py2neo import RelationshipMatcher

In [113]:
## Creating RelationMatcher on graph
rm = RelationshipMatcher(g)

In [115]:
## Collecting relationships in list
flights = list()
for cities in rm.match(r_type='FLIGHT_BETWEEN'):
    city1_name = cities.nodes[0]['name']
    city2_name = cities.nodes[1]['name']
    distance = cities['distance']
    flights.append([city1_name, city2_name, distance])

In [117]:
## Creating Pandas DataFrame
flights_df = pd.DataFrame(flights)

In [118]:
## Viewing first 5 elements in dataframe
flights_df.head()


Out[118]:
0 1 2
0 New_York San_Francisco 2572
1 New_York Dallas 1373
2 New_York Philadelphia 95
3 New_York Chicago 733
4 New_York Los_Angeles 2451