将熊猫数据框插入neo4j

时间:2020-01-17 22:53:03

标签: python pandas neo4j

我有一个熊猫数据框,例如-数据框

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 Desktop中创建的新节点。我需要交付一个项目,并且对Neo4j不太了解。谢谢。

1 个答案:

答案 0 :(得分:2)

  1. 确保查询使用正确的节点标签。您的数据库是否真的有带有标签person1person2的节点?
  2. 确保感兴趣的节点实际上具有一个名为“属性”的属性。
  3. 确保感兴趣的节点实际将“属性”值存储为整数。

[更新]

由于您已经表示您还没有任何节点,因此以下是一个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()