【问题标题】:Python loading script for neo4j returns ValueErrorneo4j 的 Python 加载脚本返回 ValueError
【发布时间】:2020-02-24 21:23:18
【问题描述】:

我对一般编程比较陌生(商业分析学生转为数据分析师),我正在测试一个 python 脚本来迭代 csv 行并为每一行构建一个密码查询以加载到 neo4j -

import pandas as pd
from neo4j import GraphDatabase

pd.set_option('display.max_colwidth', -1)

# neo4j credentials
uri= "bolt://localhost:7687"
userName= "neo4j"
password= "password"


df = pd.read_csv('C://Users/ABC/Documents/Test/Test/lineage_stored_procedure_dedup.csv', 
                 sep=',', index_col=None, header=0,usecols=[0,1,2,3,4,5])

df.columns.str.replace(' ', '')

graphDB_Driver  = GraphDatabase.driver(uri, auth=(userName, password))

with graphDB_Driver.session() as graphDB_Session:
    for row in df.iterrows():
        cq = 'merge (p:Program{programName:"'+df['Parent_Procedure']+'"}) set p.type = "'+df['Parent_Object_Type']+'"'
        res = graphDB_Session.run(cq)

graphDB_Driver.close()

我收到以下错误 -

Traceback (most recent call last):

  File "<ipython-input-91-01ba397763e3>", line 1, in <module>
    runfile('C:/Users/ABC/Documents/Test/Test/StoredProcLoadScript.py', wdir='C:/Users/ABC/Documents/Test/Test')

  File "C:\Users\ABC\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\ABC\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/ABC/Documents/Test/Test/StoredProcLoadScript.py", line 35, in <module>
    res = graphDB_Session.run(cq)

  File "C:\Users\ABC\Anaconda3\lib\site-packages\neo4j\__init__.py", line 429, in run
    if not statement:

  File "C:\Users\ABC\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1555, in __nonzero__
    self.__class__.__name__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我了解该错误意味着我没有按照 pandas 文档使用正确的按位运算符。但我不明白我什至需要在代码中从哪里开始使用它?感谢任何和所有的帮助。谢谢。

【问题讨论】:

  • 欢迎堆栈溢出!在您以cq = 开头的行中,应该是row[...] 而不是df[...]?您传递的是整个系列,而不是 iterrows() 中的行
  • @G.Anderson 非常感谢您的快速回复。我将特定的代码行重写为cq = 'merge (p:Program{programName:"'+row['Parent_Procedure']+'"}) set p.type = "'+row['Parent_Object_Type']+'"',然后我收到错误-tuple indices must be integers or slices, not str,然后我尝试将其更改为row[2]+....+row[1],然后我收到错误tuple index out of range。基本上我希望它从 csv 中的 Parent_Procedure 和 Parent_Object_Type 列中获取值并遍历这些列的行。根据我的 csv,它们分别是行索引 2 和 1。
  • @G.Anderson 我让它工作了。非常感谢您的帮助。我所要做的就是在引用行时将其更改为双引号。感谢您的帮助!

标签: python pandas neo4j cypher neo4j-driver


【解决方案1】:
#Connect to the neo4j database server
graphDB_Driver  = GraphDatabase.driver(uri, auth=(userName, password))

#CREATE NODES (:Program{Parent_Procedure}) set property 'type' = Parent_Object_Type 

with graphDB_Driver.session() as graphDB_Session:
    for index, row in df.iterrows():
        cq1 = 'merge (p:Program{programName:"'+row["Parent_Procedure"]+'"}) set p.type = "'+row["Parent_Object_Type"]+'"'
#Execute the Cypher query
        res1 = graphDB_Session.run(cq1)
        print(res1)
#CREATE NODES (:Program{Called_Procedure}) set property 'type' = Called_Object_Type 
    for index, row in df.iterrows():
        cq2 = 'merge (p:Program{programName:"'+row["Called_Procedure"]+'"}) set p.type = "'+row["Called_Object_Type"]+'"'
#Execute the Cypher query
        res2 = graphDB_Session.run(cq2)
        print(res2)

#Create relationship - (Parent_Procedure)-[:CALLS_TO]->(Called_Procedure)
    for index, row in df.iterrows():
        cq3 = 'match (p1:Program{programName:"'+row["Parent_Procedure"]+'"}) match (p2:Program{programName:"'+row["Called_Procedure"]+'"}) merge (p1)-[:CALLS_TO]->(p2)'
#Execute the Cypher query
        res3 = graphDB_Session.run(cq3)
        print(res3)        

graphDB_Driver.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-14
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多