0

This is what I believe to be a very simple question. I am trying to recreate the graph on this page (scroll down a bit):

https://linkurio.us/visualizing-network-donald-trump/

There is code to create the graph on this page but I am trying to use the py2neo equivalent. I have successfully created these same graphs in NetworkX using a syntax similar to the below code but the below code is generating something quite different. My hope is to get code that will create a proper graph in proper py2neo form from CSV files. Note that I have searched for answers and I have reviewed this link already and not sure I follow: Creating neo4j graph database from csv file using py2neo.

The below code works fine (otherwise) and I am using the latest editions of py2neo and Neo4j (CE). I prefer simple code (generators, list comprehensions, etc., may not best) but will take whatever I can get. Just so it is clear, I am reading a CSV file, reading out columns called "Organization", "Person" and "Connection" and using those values in order to create a graph showing connections as in the above link. Thank you!

Code I am using:

def readCSV_forNeo4j_Trump(path):
    with open(path + '/Trump/TrumpWorldData_Person-Org.csv', ncoding='utf8') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            Organization = row['Organization']
            Person = row['Person']
            Connection = row['Connection']
            Person_Node = Node("Person", name=Person)
            graph.create(Person_Node)
            Org_Node = Node("Organization", name=Organization)
            graph.create(Org_Node)
            Person_Org = Relationship(Person_Node, Connection, Org_Node) 
            graph.create(Person_Org)
Community
  • 1
  • 1

1 Answers1

1

You can use cypher from py2neo.

def readCSV_forNeo4j_Trump(path):
    with open(path + '/Trump/TrumpWorldData_Person-Org.csv', ncoding='utf8') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        Organization = row['Organization']
        Person = row['Person']
        Connection = row['Connection']
        graph.run("MERGE (p:Person{name:{name}}) 
        MERGE (o:Organization{name:{organization}} )
        CREATE (p)-[:IS_CONNECTED{type:{relationship}}]->(o)",
        name=Person, organization=Organization , relationship = Connection )

I have written the query of the top of my head... you can copy cypher from instructions and run it from py2neo.

Tomaž Bratanič
  • 6,319
  • 2
  • 18
  • 31
  • Thanks, Tomaz, that did the trick. One note that py2neo 3 does not have a cypher constructor. Rather than using cypher = graph.cypher, then cypher.execute (as with py2neo 2), one simply calls graph.run() (no need to instantiate a cypher object. Thanks again! – starconsult Feb 23 '17 at 01:43
  • Thank you for this. However, I have noticed that it is pretty slow. Is there any way to speed this up? I tried graph.schema.create_index but it doesn't seem to be making a difference. – Alan S. Mar 18 '17 at 00:43