我有一个 Pandas DataFrame,比如 - 数据框
Person1 Person2
933 4139
933 6597069777240
933 10995116284808
我想将它们存储到 Neo4j 中,我正在应用以下代码
from py2neo import Graph
graph = Graph("bolt://localhost:7687", user="neo4j", password="my_password")
tx = graph.begin()
for index, row in dataframe.iterrows():
tx.evaluate('''
MATCH (a:person1 {property:$Person1}), (b:person2 {property:$Person2})
MERGE (a)-[r:R_TYPE]->(b)
''', parameters = {'Person1': int(row['Person1']), 'Person2': int(row['Person2'])})
tx.commit()
代码运行没有错误,但我没有看到 Neo4j 桌面中创建的新节点。我需要交付一个项目,对 Neo4j 了解不多。谢谢。
最佳答案
person1
的节点?和 person2
? [更新]
由于您已经指出您还没有任何节点,这里是一个 Cypher 代码示例,它将创建节点和关系(同时避免意外重复):
from py2neo import Graph
graph = Graph("bolt://localhost:7687", user="neo4j", password="my_password")
tx = graph.begin()
for index, row in dataframe.iterrows():
tx.evaluate('''
MERGE (a:person1 {property:$Person1})
MERGE (b:person2 {property:$Person2})
MERGE (a)-[r:R_TYPE]->(b)
''', parameters = {'Person1': int(row['Person1']), 'Person2': int(row['Person2'])})
tx.commit()
关于python - 将 Pandas 数据框插入到 neo4j 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59795828/