gpt4 book ai didi

python - 将 Pandas 数据框插入到 neo4j 中

转载 作者:行者123 更新时间:2023-12-04 12:37:30 24 4
gpt4 key购买 nike

我有一个 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 ?
  • 确保感兴趣的节点实际上有一个名为“property”的属性。
  • 确保感兴趣的节点实际上将“属性”值存储为整数。

  • [更新]

    由于您已经指出您还没有任何节点,这里是一个 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com