# Multiple Nodes creation issue

**URL:** <https://community.neo4j.com/t/multiple-nodes-creation-issue/58442>\
**Category:** General\
**Tags:** migrated, neo4j-python-driver\
**Created:** [January 20, 2023, 3:19am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442 "2023-01-20T03:19:28Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![myrights99](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/myrights99/32/4471_2.png) [@myrights99](https://community.neo4j.com/u/myrights99)\
**Post date:** [January 20, 2023, 3:19am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/1 "2023-01-20T03:19:28Z")

</div>

Hello:  
I am using MERGE to create Node along with property (net) as below in the image. The problem is that one node is created with all the property values rather than creating multiple nodes. I am passing "net" as my dataframe column in MERGE for properties, but only one node is created. Not sure what I am missing?

![myrights99_0-1674184663761.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/5/4/54f1a5b5c111e8fd7ede0c686a3c3523a37b5f46.png "myrights99\_0-1674184663761.png")

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 20, 2023, 3:23am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/2 "2023-01-20T03:23:44Z")

</div>

can you post your query and data you are importing?

---

<div class="post-metadata">

**Author:** ![myrights99](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/myrights99/32/4471_2.png) [@myrights99](https://community.neo4j.com/u/myrights99)\
**Post date:** [January 20, 2023, 3:26am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/3 "2023-01-20T03:26:55Z")

</div>

def create\_mnc\_node(tx, pNet):  
return tx.run(  
"MERGE (n:Mnc {net: $pNet})",  
pNet=pNet  
)

session = driver.session(database="tower")

# Execute the `create_mnc_node` "unit of work" within a write transaction  
session.execute\_write(create\_mnc\_node, pNet=pNet)

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 20, 2023, 3:46am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/4 "2023-01-20T03:46:16Z")

</div>

Is pNet sent as a list? If so, and you want a new Mnc node for each element of pNet, then the following will achieve that:

```auto
forEach(p in $pNet | 
    MERGE (n:Mnc {net: p})
)

```

---

<div class="post-metadata">

**Author:** ![myrights99](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/myrights99/32/4471_2.png) [@myrights99](https://community.neo4j.com/u/myrights99)\
**Post date:** [January 20, 2023, 3:31am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/5 "2023-01-20T03:31:08Z")

</div>

This is the query that I want to make it work. but having same issues of single node as mentioned above. So i trying to create multiple nodes for one labels at a time to see if that works?

def create\_multi\_node(tx, pLat,pLon,pCell,pArea,pNet,pMcc,pCreated,pUpdated):  
return tx.run(  
"MERGE (:Geospat{lat: $pLat, lon: $pLon})\<-[:HAS\_LATLON]-(:Cid{cellID: $pCell})-[:IS\_RELATED]-\> \  
(:LAC {area: $pArea, createdatetimein\_Unix: $pCreated, updateddatetimein\_Unix:$pUpdated })- \  
[:HAS\_MNC]-\>(n:Mnc{net: $pNet})-[:IS\_ASSOCIATED]-\>(:Mcc{code: $pMcc})",  
pLat=pLat,pLon=pLon,pCell=pCell,pArea=pArea,pNet=pNet,pMcc=pMcc,pCreated=pCreated,pUpdated=pUpdated  
)

session = driver.session(database="tower")

# Execute the `create_mnc_node` "unit of work" within a write transaction  
session.execute\_write(create\_multi\_node, pLat=pLat,pLon=pLon,pCell=pCell,pArea=pArea,pNet=pNet,pMcc=pMcc,pCreated=pCreated,pUpdated=pUpdated)

---

<div class="post-metadata">

**Author:** ![myrights99](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/myrights99/32/4471_2.png) [@myrights99](https://community.neo4j.com/u/myrights99)\
**Post date:** [January 20, 2023, 4:14am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/6 "2023-01-20T04:14:59Z")

</div>

They are all dataframes

pLat = df1['lat']  
pLon = df1['lon']  
pCell = df1['cell']  
pArea = df1['area']  
pNet = df1['net']  
pMcc = df1['mcc']  
pCreated = df1['created']  
pUpdated = df1['updated']

---

<div class="post-metadata">

**Author:** ![myrights99](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/myrights99/32/4471_2.png) [@myrights99](https://community.neo4j.com/u/myrights99)\
**Post date:** [January 20, 2023, 4:11am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/7 "2023-01-20T04:11:42Z")

</div>

Thanks for your reply, but it's not working; throwing an error. (Error - ClientError: {code: Neo.ClientError.Statement.ParameterMissing} {message: Expected parameter(s): pCell})

def create\_multi\_node(tx, pCell):  
return tx.run(  
"forEach(p in $pCell | \  
MERGE (c:Cid {cellID: p}) \  
)"  
)

session = driver.session(database="tower")

# Execute the `create_mnc_node` "unit of work" within a write transaction  
session.execute\_write(create\_multi\_node, pCell=pCell)

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 20, 2023, 3:48am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/8 "2023-01-20T03:48:07Z")

</div>

Can you provide same data for one call to create\_multi\_node method?

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 20, 2023, 4:50am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/9 "2023-01-20T04:50:56Z")

</div>

What about the larger query? Do you need to fix that too?

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 20, 2023, 4:14am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/10 "2023-01-20T04:14:26Z")

</div>

You did not pass pCell to the 'run' method.

```auto
def create_multi_node(tx, pCell):
return tx.run(
"forEach(p in $pCell | \
MERGE (c:Cid {cellID: p}) \
), pCell = pCell"
)

```

---

<div class="post-metadata">

**Author:** ![myrights99](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/myrights99/32/4471_2.png) [@myrights99](https://community.neo4j.com/u/myrights99)\
**Post date:** [January 20, 2023, 6:56am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/11 "2023-01-20T06:56:21Z")

</div>

I have data for all the nodes, as shown below. If you can help me how to fix that, it will be great.

def create\_multi\_node(tx, pLat,pLon,pCell,pArea,pNet,pMcc,pCreated,pUpdated):  
return tx.run(  
"MERGE (:Geospat{lat: $pLat, lon: $pLon})\<-[:HAS\_LATLON]-(:Cid{cellID: $pCell})-[:IS\_RELATED]-\> \  
(:LAC {area: $pArea, createdatetimein\_Unix: $pCreated, updateddatetimein\_Unix:$pUpdated })- \  
[:HAS\_MNC]-\>(n:Mnc{net: $pNet})-[:IS\_ASSOCIATED]-\>(:Mcc{code: $pMcc})",  
pLat=pLat,pLon=pLon,pCell=pCell,pArea=pArea,pNet=pNet,pMcc=pMcc,pCreated=pCreated,pUpdated=pUpdated  
)

---

<div class="post-metadata">

**Author:** ![myrights99](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/myrights99/32/4471_2.png) [@myrights99](https://community.neo4j.com/u/myrights99)\
**Post date:** [January 20, 2023, 4:27am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/12 "2023-01-20T04:27:14Z")

</div>

I appreciate your help; finally, it is working. Is there a better way to create required multinodes based on labels, or do you recommend creating individual label (multinodes) and then creating relationships?

Thank you again for your help.

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 20, 2023, 4:50am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/13 "2023-01-20T04:50:17Z")

</div>

If you all the data, it may be easier to add the nodes and relationships at the same time. What is your circumstance?

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 21, 2023, 7:50pm UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/14 "2023-01-21T19:50:00Z")

</div>

I am not sure this is what you want. I followed your query, but changed it to work from what I gathered you wanted.

```auto
load csv with headers from "file:///IndiaTowerDump_Data_2000_Records.csv" as row
merge(geo:Geospat{lat: row.lat, lon: row.lon})
merge(cid:Cid{cellID: row.cell})
merge(lac:LAC {area: row.area, createdatetimein_Unix: row.created, updateddatetimein_Unix: row.updated })
merge(mnc:Mnc{net: row.net})
merge(mcc:Mcc{code: row.mcc})
merge (geo)<-[:HAS_LATLON]-(cid)-[:IS_RELATED]->(lac)-[:HAS_MNC]->(mnc)-[:IS_ASSOCIATED]->(mcc)

```

Graph resulting from your data:

 ![Screen Shot 2023-01-21 at 2.45.47 PM.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/b/b/bb7027c05cc501d6e72b96f8ea08641897fef692.png "Screen Shot 2023-01-21 at 2.45.47 PM.png")

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 21, 2023, 8:59pm UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/15 "2023-01-21T20:59:14Z")

</div>

I just realized the script imports all the values as a 'string'. This is the default behavior. You may want numerical values for most of these, since they look like numbers in the spreadsheet. You need to wrap each row property in toInteger() or toFloat() to convert them during the import.

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 21, 2023, 9:59pm UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/16 "2023-01-21T21:59:17Z")

</div>

I realized one more thing. If you merge all the nodes together with one merge statement, than a new path is made between each node if one or more relationships is missing. This is because a merge operation is all or nothing. As such, I decomposed the merging of the path into individual merges between each pair of nodes. I also added the conversion to numbers in this version.

```auto
load csv with headers from "file:///IndiaTowerDump_Data_2000_Records.csv" as row

merge(geo:Geospat{lat: toFloat(row.lat), lon: toFloat(row.lon)})
merge(cid:Cid{cellID: toInteger(row.cell)})
merge(geo)<-[:HAS_LATLON]-(cid)

merge(lac:LAC {area: toInteger(row.area), createdatetimein_Unix: toInteger(row.created), updateddatetimein_Unix: toInteger(row.updated)})
merge(cid)-[:IS_RELATED]->(lac)

merge(mnc:Mnc{net: toInteger(row.net)})
merge(lac)-[:HAS_MNC]->(mnc)

merge(mcc:Mcc{code: toInteger(row.mcc)})
merge(mnc)-[:IS_ASSOCIATED]->(mcc)

```

Ignore the earlier graph as well.

---

<div class="post-metadata">

**Author:** ![myrights99](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/myrights99/32/4471_2.png) [@myrights99](https://community.neo4j.com/u/myrights99)\
**Post date:** [January 22, 2023, 2:32am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/17 "2023-01-22T02:32:34Z")

</div>

Thank you for your excellent help. When I do print (df.dtypes) in my jupyter notebook. below is the output. So I am trying to understand how you are getting it as a 'String'.??

radio object  
mcc int64  
net int64  
area int64  
cell int64  
unit int64  
lon float64  
lat float64  
range int64  
samples int64  
changeable int64  
created int64  
updated int64  
averageSignal int64  
ID int64

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 22, 2023, 2:37am UTC](https://community.neo4j.com/t/multiple-nodes-creation-issue/58442/18 "2023-01-22T02:37:48Z")

</div>

I added the conversion in the last query version.
