Neo4j 使用 py2neo 从 pandas 数据帧创建节点和关系 [英] Neo4j create nodes and relationships from pandas dataframe with py2neo

查看:27
本文介绍了Neo4j 使用 py2neo 从 pandas 数据帧创建节点和关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 py2neo 从 Neo4j 数据库的密码查询中获取 Pandas 数据帧的结果非常简单,如:

<预><代码>>>>从熊猫导入数据帧>>>DataFrame(graph.data("MATCH (a:Person) RETURN a.name, a.born LIMIT 4"))a.出生 a.name0 1964 基努·里维斯1 1967 年凯莉-安妮·莫斯2 1961 劳伦斯·菲什伯恩3 1960 年雨果织布

现在我正在尝试使用 py2neo 创建(或更好地合并)一组从 Pandas 数据帧到 Neo4j 数据库的节点和关系.想象一下,我有一个像这样的数据框:

LABEL1 LABEL2p1 n1p2 n1p3 n2p4 n2

其中标签是列标题,属性是值.我想为我的数据帧的每一行重现以下密码查询(以第一行为例):

query="""匹配 (a:Label1 {property:p1))合并 (a)-[r:R_TYPE]->(b:Label2 {property:n1))"""

我知道我可以只告诉 py2neo graph.run(query),甚至以相同的方式运行 LOAD CSV 密码脚本,但我想知道我是否可以遍历数据框并逐行应用上述查询 WITHIN py2neo.

解决方案

您可以使用 DataFrame.iterrows() 遍历 DataFrame 并对每一行执行查询,从行作为参数.

 用于索引,df.iterrows() 中的行:graph.run('''匹配 (a:Label1 {property:$label1})合并 (a)-[r:R_TYPE]->(b:Label2 {property:$label2})''', parameters = {'label1': row['label1'], 'label2': row['label2']})

这将每行执行一个事务.我们可以将多个查询批处理到一个事务中以获得更好的性能.

tx = graph.begin()对于索引,df.iterrows() 中的行:tx.evaluate('''匹配 (a:Label1 {property:$label1})合并 (a)-[r:R_TYPE]->(b:Label2 {property:$label2})''', parameters = {'label1': row['label1'], 'label2': row['label2']})tx.commit()

通常我们可以在单个事务中批量处理 ~20k 数据库操作.

Getting results on a pandas dataframe from a cypher query on a Neo4j database with py2neo is really straightforward, as:

>>> from pandas import DataFrame
>>> DataFrame(graph.data("MATCH (a:Person) RETURN a.name, a.born LIMIT 4"))
   a.born              a.name
0    1964        Keanu Reeves
1    1967    Carrie-Anne Moss
2    1961  Laurence Fishburne
3    1960        Hugo Weaving

Now I am trying to create (or better MERGE) a set of nodes and relationships from a pandas dataframe into a Neo4j database with py2neo. Imagine I have a dataframe like:

LABEL1 LABEL2
p1 n1
p2 n1
p3 n2
p4 n2

where Labels are column header and properties as values. I would like to reproduce the following cypher query (for the first row as example), for every rows of my dataframe:

query="""
    MATCH (a:Label1 {property:p1))
    MERGE (a)-[r:R_TYPE]->(b:Label2 {property:n1))
"""

I know I can tell py2neo just to graph.run(query), or even run a LOAD CSV cypher script in the same way, but I wonder whether I can iterate through the dataframe and apply the above query row by row WITHIN py2neo.

解决方案

You can use DataFrame.iterrows() to iterate through the DataFrame and execute a query for each row, passing in the values from the row as parameters.

for index, row in df.iterrows():
    graph.run('''
      MATCH (a:Label1 {property:$label1})
      MERGE (a)-[r:R_TYPE]->(b:Label2 {property:$label2})
    ''', parameters = {'label1': row['label1'], 'label2': row['label2']})

That will execute one transaction per row. We can batch multiple queries into one transaction for better performance.

tx = graph.begin()
for index, row in df.iterrows():
    tx.evaluate('''
      MATCH (a:Label1 {property:$label1})
      MERGE (a)-[r:R_TYPE]->(b:Label2 {property:$label2})
    ''', parameters = {'label1': row['label1'], 'label2': row['label2']})
tx.commit()

Typically we can batch ~20k database operations in a single transaction.

这篇关于Neo4j 使用 py2neo 从 pandas 数据帧创建节点和关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆