Load CSV with empty cells

Hello everyone, I am trying to load simple data from CSV to Noe4j, but it keeps giving me this error
"Neo.ClientError.Statement.SemanticError: Cannot merge node using null property value for Name"

I don't have null values in my file !!. Even if I will have, how to deal with it?
This is the code

Blockquote

LOAD CSV WITH HEADERS FROM "file:///new.csv" as line
MERGE (p:per {Name:line.person})
MERGE (m:mang {Name:line.mang})
MERGE (p) -[:S {Name:line.Score}]-> (m)

Blockquote
and this is my csv file
image

As for nulls, null in Neo4j is the equivalent of there being no property at all. In this case I'm guessing you have blank lines in your CSV. You may need to inspect it in a text editor and delete the blank lines.

You can always get around this by adding a WHERE clause into the query:

LOAD CSV WITH HEADERS FROM "file:///new.csv" as line
WITH line
WHERE line.person IS NOT NULL
MERGE (p:per {Name:line.person})
MERGE (m:mang {Name:line.mang})
MERGE (p) -[:S {Name:line.Score}]->(m)
2 Likes
  1. Do you have an extra spaces or return at the end of the file ?
  2. Do you want to skip all the empty persons ?
  3. Do you want to replace any empty persons with some "NA" data ?
2 Likes

Thanks for the prompt response.
I found the issue with Microsoft Excel and solved it.
but I have another case which looks like the following:

LOAD CSV WITH HEADERS FROM "file:///new.csv" as line
MERGE (p:per {Name:line.person})
MERGE (m:mang {Name:line.mang})
MERGE (c:com {Name:line.comp})
MERGE (p) -[:S {Name:line.Score}]-> (m)
MERGE (p) -[:Works_For ]-> (c)

This is the csv file
image

in this case, I want to create that graph like this
image
(i.e I want to create the partially filed record (row #3 in my file) without creating a node for the empty cell in "comp" column)
"I don't want to neglect the whole record in case it contains some empty cells"

Thanks for the reply.
I solved the leading/extra spaces issue but now my concern is what I mentioned in my last reply.

For any lines that may contain nulls, you can check before your MERGE statement (note the closing parentheses at the end):

FOREACH (ignoreMe in CASE WHEN exists(line.comp) THEN [1] ELSE [] END |MERGE (c:com {Name:line.comp}) MERGE (p) -[:Works_For ]-> (c))
FOREACH (ignoreMe in CASE WHEN exists(line.Score) THEN [1] ELSE [] END |MERGE (m:mang {Name:line.mang}) MERGE (p) -[:S {Name:line.Score}]-> (m))
1 Like

pdrangeid
Thank you very much, I tried it and it is working now :slight_smile:

Hi paul, I tried running your code, i have about 9 properties but the first 7 do not have empty spaces.When I run the code as shared it works for the first set of rows of data that has the 8th property value missing but the problems comes when it iterates the second sets of row. I was hoping the for each in case acts like a for loop. would appreciate your help

hi ahmed, i seem to have run into the same problem, any assistance?

The foreach in case is actually behaving more like an if/then/else statement.
To get true loop behavior in cypher you typically have to use an UNWIND or apoc.periodic.commit

It does sort-of loop (The foreach in case will be evaluated for EACH ROW/MATCH preceding it). In your CASE WHEN condition you can add multiple validations (if this is not null, and that is not null) etc.

The other tricky part to keep in mind is that any variables generated within the statements after the pipe are only available within the parentheses. It will not exist AFTER you close the paren (so you can't use the (c) node from the MERGE statement within the FIRST FOREACH in a subsequent FOREACH unless you add a WITH, and then perform an OPTIONAL MATCH (or often in this case an OPTIONAL MATCH) to capture the variable for use in the subsequent parts of the CYPHER transaction.

If you need a more specific example, please post some sample of the IMPORT data and the CYPHER code you are using and I can take a look.

Good morning Paul,
I created a neo4j graph created using the following parameters, which are working fine.This was created using python.
query = """ merge (Name:Name {Name: {a}, Type:{b}, Site:{c}, Title:{d}, Other_names:{e}, Active:{f}})
class.run(query, {"a": row[0], "b": row[1], "c": row[2], "d": row[3], "e": row[4],
"f": row[5], "g": row[6]})
(6 columns). My challenge is to merge it with the below query which only has 3 columns associated with the query with 6 properties .These are Name,Type and Title

query = """
merge(name:name {Name: {a},Type:{b},Title:{c},
status:{d},number:{e},Code:{f},Country:{g},vicinity:{h},Address:{i}})
"""
class.run(query,{"a": Name, "b": Type, "c":Title, "d": status, "e":number, "f":Code,
"g": Country,"h": vicinity,"i":Address})
I want to merge the new query with columns a to i to the existing nodes with columns a to e in the already created database but it doesn't merge .I want to be able to add the new set of properties from the new query to the initial database .It gives this error
py2neo.database.ClientError: SemanticError: Cannot merge node using null property value for Country.
but when i use merge in the same query to create a new graph database,it works without complaining about null properties as long as the initially created database is cleaned out. Please how do I merge my new query to the existing database.
thanks

You can't merge using null values.. You can either merge without the null value (then set properties afterwards, or merge and use coalesce to replace null with a placeholder: coalesce({g},'none')

Not a python coder, but check the cypher coalesce statement..

Thank you paul.Would take time to rework the code.

Sorry olojolabamiji, I didn't notice your comment.
Hope your problem has been solved.
O.W I am here to help.

Hi ahmed. Thank you I have been able to solve it. I had to rewrite my code using the SET method.Still on it though.

1 Like