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]:
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)
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]: