I'm trying to export data from python to neo4j db using py2neo package.
I've a dataframe'ranked_conts' containing a number content is which are string and their corresponding ranks which are in float.
my code is
findnode = list(graph.find('rank',property_key='type',property_value='TrendingRank'))
if len(findnode) > 0:
print("already exist")
else:
for i in range(len(ranked_conts)):
conts = ranked_conts.iloc[i]['content_id']
rank = ranked_conts.iloc[i]['rank']
graph.run("MERGE(c:Content{contentId:"+str(conts)+"})-[hr: HAS_RANK{r:"+str(rank)+"}]->(rank:Rank {type: 'TrendingRank'})")
but when I execute it I'm getting an error message as py2neo.database.status.CypherSyntaxError: Variable EROS_6272897
not defined.
You want to use parameters instead of creating a new query for every i in range
. Something like:
findnode =list(graph.find('rank',property_key='type',property_value='TrendingRank'))
if len(findnode) > 0:
print("already exist")
else:
for i in range(len(ranked_conts)):
conts = ranked_conts.iloc[i]['content_id']
rank = ranked_conts.iloc[i]['rank']
graph.run("MERGE (c:Content{contentId:{cId}})-[hr: HAS_RANK{r:{rankValue}}]->(rank:Rank {type: 'TrendingRank'})",cId = str(conts),rankValue = str(rank))
This should speed up your query and make it easier to check for syntax error. One note is also you probably want to merge :Content
and :Rank
separately and then MERGE
the relationship on to it. Like this.
MERGE (c:Content{contentId:{cId}})
MERGE (rank:Rank {type: 'TrendingRank'})
MERGE (c)-[hr: HAS_RANK{r:{rank}}]->(rank)