python操作neo4j处理药材数据实例

1,420 阅读2分钟

py2neo的使用例子,记录本人学习neo4j数据库时使用python操作的实例,从数据导入生成图,到数据查询导出进行数据处理

连接neo4j数据库

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'))

清空数据库 graph.run(cypher="Match(n) detach delete n") #清空数据库

导入数据

matcher = NodeMatcher(graph)
file = 'neo4jdata/entity-01.csv'
if not os.path.exists(file):
    print('{} 文件不存在'.format(file))
df=pd.read_csv(file)
df = df.fillna(value=str('不存在'))
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)

image.png

image.png

file = 'neo4jdata/entity-02.csv'
if not os.path.exists(file):
    print('{} 文件不存在'.format(file))
df=pd.read_csv(file)
df = df.fillna(value=str('不存在'))
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)

image.png

image.png

file = 'neo4jdata/rela-11.csv'
if not os.path.exists(file):
    print('{} 文件不存在'.format(file))
df = pd.read_csv(file)
df = df.fillna(value=str('不存在'))
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]).first()
    to_id=matcher.match("enterprise",id=data[8]).first()


    thing={"药材名称:":data[2]}
    price_one={"单价(元/千克):":data[5]}
    num={"重量:":data[6]}
    total={"总计:":data[7]}

    graph.create(Relationship(from_node, data[1], to_id,**thing,**price_one,**num,**total))

image.png

image.png

image.png

file = 'neo4jdata/rela-12.csv'
if not os.path.exists(file):
    print('{} 文件不存在'.format(file))
df = pd.read_csv(file)
df = df.fillna(value=str('不存在'))
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]).first()
    to_id=matcher.match("herb",id=data[5]).first()


    thing={"交易物品:":data[2]}
    provider={"供应商":data[4]}

    graph.create(Relationship(from_node, data[1], to_id,**provider,**thing))

image.png

image.png

file = 'neo4jdata/rela-21.csv'
if not os.path.exists(file):
    print('{} 文件不存在'.format(file))
df = pd.read_csv(file)
df = df.fillna(value=str('不存在'))
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]).first()
    to_id=matcher.match("herb",id=data[3]).first()

    thing={"交易物品:":data[1]}

    graph.create(Relationship(from_node, data[2], to_id,**thing))

image.png

image.png

数据使用

#查找所有买入记录
show_data = graph.run("match ()-[r:买入]-() return distinct r")
for i in show_data:
    print(i)

image.png

#查找所有卖出记录
show_data = graph.run("match ()-[r:卖出]-() return distinct r")
for i in show_data:
    print(i)

image.png

#查询交易记录
show_data = graph.run("match ()-[r:交易]-() return distinct *")
for i in show_data:
    print(i)

image.png

#统计药材交易总金额
import re
show_data = graph.run("MATCH (n) WHERE EXISTS(n.`总计:`) RETURN DISTINCT 'node' as entity, n.`总计:` AS `总计:`  UNION ALL MATCH ()-[r]-() WHERE EXISTS(r.`总计:`) RETURN DISTINCT 'relationship' AS entity, r.`总计:` AS `总计:`  ")
total_hurb=0
for i in show_data:
    total_hurb+=int(i[1])
    
print("药材订单总金额为:"+str(total_hurb)+"元")

image.png

#统计药材交易总重量
import re
show_data = graph.run("MATCH (n) WHERE EXISTS(n.`重量:`) RETURN DISTINCT 'node' as entity, n.`重量:` AS `重量:`  UNION ALL MATCH ()-[r]-() WHERE EXISTS(r.`重量:`) RETURN DISTINCT 'relationship' AS entity, r.`重量:` AS `重量:` ")
total_hurb=0
for i in show_data:
    total_hurb+=int(i[1])
    
print("药材交易总重量为:"+str(total_hurb)+"千克")

image.png

数据集需要的话可以在评论区提出,我再找个方式发出来。