# First Create a node and then merge in Neo4j?

**URL:** <https://community.neo4j.com/t/first-create-a-node-and-then-merge-in-neo4j/15620>\
**Category:** Neo4j Graph Platform\
**Tags:** apoc, cypher\
**Created:** [March 11, 2020, 6:21am UTC](https://community.neo4j.com/t/first-create-a-node-and-then-merge-in-neo4j/15620 "2020-03-11T06:21:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishnuvardhans1698](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/vishnuvardhans1698/32/8227_2.png) [@vishnuvardhans1698](https://community.neo4j.com/u/vishnuvardhans1698)\
**Post date:** [March 11, 2020, 6:21am UTC](https://community.neo4j.com/t/first-create-a-node-and-then-merge-in-neo4j/15620/1 "2020-03-11T06:21:19Z")

</div>

I have a dataframe in pandas for which i'm trying to create a graph. Here's the data.

```auto
**source**  **destination**  **type**
London New York Arrival
New York Dubai Departure
Manchester Visakhapatnam Departure
New Delhi London Departure
Visakhapatnam Dubai Arrival

```

I created a list in pandas and got all the unique places into a list like this:  
places = ['London', 'New York', .......]  
and then converted it into a datafame and created nodes using this query:

```auto
def add_unique_places(tx,df_places):
    for index,row in df_places.iterrows():
        tx.run("""
               CREATE (place:P {name: $label1})
               """,
               parameters={'label1':row['Places']})

```

Now i divided the data into two dataframes. One for type "Arrival" and one for type "Departure".

All i want now is to create a graph like this:  
(London)-[:Arrival]-\>(New York)

How to achieve this?

PS:  
WIthout doing all the above process, i directly took the dataframe and created a graph and merged nodes using apoc. But theres a problem.  
Since "London" is in both 'source' and 'destination', it's creating two different nodes but i only want one node.  
Thank You!  
Peace! 🙂

---

<div class="post-metadata">

**Author:** ![intouch\_vivek](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/intouch_vivek/32/4097_2.png) [@intouch\_vivek](https://community.neo4j.com/u/intouch_vivek)\
**Post date:** [March 11, 2020, 6:58am UTC](https://community.neo4j.com/t/first-create-a-node-and-then-merge-in-neo4j/15620/2 "2020-03-11T06:58:55Z")

</div>

Hi @vishnuvardhans1698,

Sorry, I did not understand your requirement completely.  
However with my best understanding with the dataframe you have given try below

load csv with headers from 'file:///travel.csv' as line  
Merge(n:Place{name:line.source}) Merge(m:Place{name:line.destination}) Merge (n)-[:TRAVEL\_TYPE{type:line.type}]-\>(m)

---

<div class="post-metadata">

**Author:** ![vishnuvardhans1698](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/vishnuvardhans1698/32/8227_2.png) [@vishnuvardhans1698](https://community.neo4j.com/u/vishnuvardhans1698)\
**Post date:** [March 11, 2020, 7:16am UTC](https://community.neo4j.com/t/first-create-a-node-and-then-merge-in-neo4j/15620/3 "2020-03-11T07:16:56Z")

</div>

I think it's doing the work. Thank you vivek!

---

<div class="post-metadata">

**Author:** ![vishnuvardhans1698](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/vishnuvardhans1698/32/8227_2.png) [@vishnuvardhans1698](https://community.neo4j.com/u/vishnuvardhans1698)\
**Post date:** [March 11, 2020, 9:06am UTC](https://community.neo4j.com/t/first-create-a-node-and-then-merge-in-neo4j/15620/4 "2020-03-11T09:06:02Z")

</div>

Also, i have one more question.  
Suppose i have two relations like this:  
(london)-[:DEPARTURE{time:10:30}]-\>(newYork)  
(vizag)-[:ARRIVAL{time:10:30}]-\>(Delhi)

Now i want all nodes with time:10:30 whether the relation is "ARRIVAL" or "DEPARTURE".  
what is the query for this? Thank you.  
Peace 🙂

---

<div class="post-metadata">

**Author:** ![intouch\_vivek](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/intouch_vivek/32/4097_2.png) [@intouch\_vivek](https://community.neo4j.com/u/intouch_vivek)\
**Post date:** [March 11, 2020, 9:18am UTC](https://community.neo4j.com/t/first-create-a-node-and-then-merge-in-neo4j/15620/5 "2020-03-11T09:18:02Z")

</div>

(n)-[rel:ARRIVAL|:DEPARTURE]-\>(m) Where rel.time='10:30' Return n,rel,m
