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.

First Create a node and then merge in Neo4j?

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

**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:

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!

1 ACCEPTED SOLUTION

intouch_vivek
Graph Steward

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)

View solution in original post

4 REPLIES 4

intouch_vivek
Graph Steward

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)

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

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

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