cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Not able to pass pandas dataframe to neo4j via cypher

Hi All

I am new to cypher or neo4j and still trying to learn the craft. I am stuck on an issue. I have a dataframe which I want to transfer to neo4j graph using cypher. Here are the details


Getting ValueError: dictionary update sequence element #0 has length 3; 2 is required

What I am trying to achieve via cypher query

import pandas as pd
from py2neo import Graph,Node,Relationship
from neo4j import GraphDatabase, basic_auth

for index, row in df.iterrows():
    tx = graph.begin()
    tx.evaluate('''
       Merge (W1:word{term:row.Word1)
       Merge(W2:word{term:row.Word2)
       Merge (W1)-[r:Next]->(W2)
       set r.start = row.POS1
       set r.end = row.POS2
       ''',parameters = {row.Word1, row.Word2})
    tx.commit()

Objective: The "next" relationship has a direction and I am attaching the POS values into the specific relationships. I would then create the word nodes with the unique property being term. In this way I wish to achieve the words are connected and I have the POS in the relation properties.

11 REPLIES 11

parameters = {"row" : row}

Hey Sanjay thanks for you response I tried your suggestion but I am still getting error stating----> TypeError: Neo4j does not support JSON parameters of type Series

Sorry. Try this.
tx.evaluate('''
Merge (W1:word{term:$Word1)
Merge(W2:word{term:$Word2)
Merge (W1)-[r:Next]->(W2)
set r.start = $POS1
set r.end = $POS2
''',parameters = {"Word1":row.Word1, "Word2": row.Word2,"POS1":row.POS1, "POS2":row.POS2})
tx.commit()

@sanjaysingh13

This failed again. I tried couple of more things but didn't work for me.
I am attaching the query so that you can replicate this at your end

from py2neo import Graph,Node,Relationship
from neo4j import GraphDatabase, basic_auth

for index, row in df.iterrows():
    tx = graph.begin()
    tx.evaluate('''
    Merge (W1:word{term:$Word1)
    Merge(W2:word{term:$Word2)
    Merge (W1)-[r:Next]->(W2)
    set r.start = $POS1
    set r.end = $POS2
    ''',parameters = {"Word1":row.Word1, "Word2": row.Word2,"POS1":row.POS1, "POS2":row.POS2})
tx.commit()

Dataframe is like

3X_7_4_74a7251a173451d708c3fa73ab3dd7011b402bcd.png

It’s a simple typo. There’s a missing closing curly brace at the location pointed in error message. In the next line too.

@sanjaysingh13 .Thanks for pointing that out. It was a miss from my end apologies. I was able to execute that now. However, looking at the results the POS did not get populated. The output I got has

{
  "identity": 26,
  "labels": [
    "word"
  ],
  "properties": {
"term": "his"
  }
}

{
  "identity": 27,
  "labels": [
    "word"
  ],
  "properties": {
"term": "dog"
  }
}

{
  "identity": 28,
  "labels": [
    "word"
  ],
  "properties": {
"term": "eats"
  }
}

{
  "identity": 29,
  "labels": [
    "word"
  ],
  "properties": {
"term": "turkey"
  }
}

{
  "identity": 30,
  "labels": [
    "word"
  ],
  "properties": {
"term": "on"
  }
}

{
  "identity": 31,
  "labels": [
    "word"
  ],
  "properties": {
"term": "tuesday"
  }
}

The idea is to attach the POS in properties and label as words

Hi,
Can you mail me a copy of df? Just pickle it.

@sanjaysingh13

Please find the file attached

change file extn to .csv
sentence.txt (132 Bytes)

Are you sure you want Nodes like (:his {prop:"PRON"}), (:dog {prop:"NOUN"})? Please share what you expect your Graph to look like. Even a pencil-sketch would be fine.

@sanjaysingh13
Sure I will try to explain here.
Main task mentioned in the bullets

  1. I want to attach POS against each word maintaining the "Next" relationship of sentence
  2. Different sentences will have different POS for the same word ( So it will be better if we can create a collection of properties for each word)

The final goal is to make a graph in such way where I search a word and I get what all POS it has been used in basically to get the context.

For now I just want 2 things

  1. Given the pandas dataframe can I put the same in neo4j graph with the NEXT relationship between word1 & word2
  2. For each word label for ex Dog attach both the POS2 and POS1 value as a collection of property

The ideal way to model your requirements would be to have Sentence, Word and Function (POS). You don't want to repeat POS as a property because POS is generic.
(:Word {term:"dog")-[:APPEARS_AS]-(p1:POS)-[:IN]-(:Sentence {phrase:"The dog ate the book")
(:Word {term:"ate")-[:APPEARS_AS]-(p2:POS)-[:IN]-(:Sentence {phrase:"The dog ate the book")
(:Word {term:"dog")-[:NEXT]-(:Word {term:"ate")
(p1)-[:IS_PART_OF_SPEECH]-(:Function {pos:"Subject"})
(p2)-[:IS_PART_OF_SPEECH]-(:Function {pos:"Verb"})