The example of py2neo was used to record the example of Python operation when I learned neo4j database, from data import to generate graphs to data query and export for data processing
Connect to the Neo4j database
from py2neo import Graph, Node, Relationship, NodeMatcher
from pandas import DataFrame
import os
import pandas as pd
graph = Graph('http://localhost:7474', auth=('neo4j', 'test'))
Copy the code
Run (cypher=”Match(n) detach delete n”
Import data
matcher = NodeMatcher(graph) file = 'neo4jdata/entity-01.csv' if not os.path.exists(file): Print ('{} file not found '. Format (file)) df=pd.read_csv(file) df= df.fillna(value= STR (' not found ')) print(df) a =df['id'] b =df['name'] c =df['label'] for data in zip(a,b,c): print(data) node = Node('enterprise', name=data[1]) node['id']=data[0] node['type']=data[2] graph.create(node)Copy the code
file = 'neo4jdata/entity-02.csv' if not os.path.exists(file): Print ('{} file not found '. Format (file)) df=pd.read_csv(file) df= df.fillna(value= STR (' not found ')) print(df) a =df['id'] b =df['name'] c =df['label'] for data in zip(a,b,c): print(data) node = Node('herb', name=data[1]) node['id']=data[0] node['type']=data[2] graph.create(node)Copy the code
file = 'neo4jdata/rela-11.csv' if not os.path.exists(file): Format (file) df = pd.read_csv(file) df = df.fillna(value= STR (' not present ')) print(df) col=df.columns print(col) for data in zip( df[col[0]],df[col[1]],df[col[2]] , df[col[3]], df[col[4]], df[col[5]], df[col[6]], df[col[7]], df[col[8]]): print(data) from_node=matcher.match("enterprise", Id =data[0]).to_id =matcher.match("enterprise",id=data[8]).to_id =matcher.match("enterprise", ID =data[2]).to_id =matcher.match("enterprise", ID =data[8]). ":data[5]} num={" :data[6]} total={" :data[5]} num={" :data[6]} total={" ":data[7]} graph.create(Relationship(from_node, data[1], to_id,**thing,**price_one,**num,**total))Copy the code
file = 'neo4jdata/rela-12.csv' if not os.path.exists(file): Format (file) df = pd.read_csv(file) df = df.fillna(value= STR (' not present ')) print(df) col=df.columns print(col) for data in zip( df[col[0]],df[col[1]],df[col[2]] , df[col[3]], df[col[4]], df[col[5]]): print(data) from_node=matcher.match("enterprise", Id = data [0]). The first () to_id = the matcher. Match (" herb ", id = data [5]). The first thing () = {" trading items: Graph. Create (Relationship(from_node, data[1], to_id,**provider,**thing))Copy the code
file = 'neo4jdata/rela-21.csv' if not os.path.exists(file): Format (file) df = pd.read_csv(file) df = df.fillna(value= STR (' not present ')) print(df) col=df.columns print(col) for data in zip( df[col[0]],df[col[1]],df[col[2]] ,df[col[3]]): print(data) from_node=matcher.match("enterprise", Id = data [0]). The first () to_id = the matcher. Match (" herb ", id = data [3]). The first thing () = {" trading items: ":data[1]} graph.create(Relationship(from_node, data[2], to_id,**thing))Copy the code
The data used
Show_data = graph.run("match ()-[r: buy]-() return distinct r") for I in show_data: print(I)Copy the code
Show_data = graph.run("match ()-[r: sell]-() return distinct r") for I in show_data: print(I)Copy the code
Show_data = graph.run("match ()-[r: transaction]-() return distinct *") for I in show_data: print(I)Copy the code
Import re show_data = graph.run("MATCH (n) WHERE EXISTS(n. 'totals:') RETURN DISTINCT 'node' as entity, n. 'totals: 'AS' total: 'UNION ALL MATCH ()-[r]-() WHERE EXISTS(r.' Total: ') RETURN DISTINCT 'relationship' AS entity, r. 'Total:' AS 'total:' Total_hurb =0 for I in total_data: total_hurb+=int(I [1]) print("Copy the code
Import re show_data = graph.run("MATCH (n) WHERE EXISTS(n. 'weight:') RETURN DISTINCT 'node' as entity, n. 'weight: UNION ALL MATCH ()-[r]-() WHERE EXISTS(r. 'weight:') RETURN DISTINCT 'relationship' AS entity, r. 'weight:' AS 'weight:' Total_hurb =0 for I in total_data: total_hurb+=int(I [1]) print("Copy the code
Data sets can be posted in the comments section if needed, and I’ll find another way to post them.