I have the output as a pandas data frame. I am trying to write a cypher query to give this data frame as the input to Neo4j and create a graph with relations extracted from its source and target.
If anyone has any idea on how to proceed further, please guide me in this.
[Pandas Dataframe]
The key is how you pass the panda dataframe into the query. I used string format to parameterized source, target and relationship type.
from py2neo import Graph
df = pd.DataFrame([['Kimbal','sibling', 'Elon'],
['Boring', 'owned_by', 'Elon']],
columns=['source', 'relation_type', 'target'])
query = """
MERGE (s:Source {{name: '{source}'}})
MERGE (t:Target {{name: '{target}'}})
MERGE (s)-[r:{relation_type}]->(t)
RETURN s.name,t.name,type(r) as relation_type;"""
graph = Graph("bolt://localhost:7687", auth=("neo4j", "awesome_password"))
for d in df.values:
result = graph.run(query.format(source=d[0], relation_type=d[1], target=d[2]))
d = result.data()
print(d)
Result:
[{'s.name': 'Kimbal', 't.name': 'Elon', 'relation_type': 'sibling'}]
[{'s.name': 'Boring', 't.name': 'Elon', 'relation_type': 'owned_by'}]