Search code examples
python-3.xpandasdataframeneo4jpy2neo

Is there a way to store dynamic Cypher query into python variable?


Is there a way to put my cypher query into a python variable? I am able to put the non-parameter of the cypher into a variable but getting the error [![enter image description here][1]][1]

[1]: https://i.sstatic.net/mfRrp.png on trying to put the parameter part into a variable.

I am using pandas and py2neo to load a CSV into neo4j. df_data is my pandas dataframe with 2 columns and 3 rows.

for index,row in df_data.iterrows():
        tx.evaluate('''MERGE (x:PATIENT_ID {property:$PATIENT_ID})
            MERGE(y:GLB_ID {property:$GLB_ID})
            MERGE (x)-[r:R_TYPE]->(y) ''', parameters = {'PATIENT_ID': int(row['PATIENT_ID']), 'GLB_ID': int(row['GLB_ID'])})

My code runs without issues if I run the following code storing non-parameter part into cq:

for index,row in df_data.iterrows():
        tx.evaluate(cq, parameters = {'PATIENT_ID': int(row['PATIENT_ID']), 'GLB_ID': int(row['GLB_ID'])})

I am looking for a way to store the parameter part into a python variable


Solution

  • I used dictionary to get this resolved. I used dictionary over the cypher query to get it resolved:

    for index,row in df_data.iterrows():
            t_dict = {}
            for k in p_dict.keys():
                try: 
                    t_dict[k] = int(row[k])
                except TypeError:
                    t_dict[k] = row[k]
            tx.evaluate(cq,t_dict)