python将csv文件导入neo4j数据库中

2022/6/12 6:20:26

本文主要是介绍python将csv文件导入neo4j数据库中,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

其实就是按行解析csv文件,并将其中数据分为“结点”、“关系”两种类型,构建利用Node()方法构建节点;Relationship()方法构建关系

csv结构

entity1,relation1,entity2,relation2,info,relation3,keyword
人格纠纷权,包含,一般人格权纠纷,描述,一般人格权纠纷是指因侵害他人的一般人格权,使他人的人格利益受损而引起的纠纷。,关键词,人格权、纠纷、指因、人格、利益

 

from py2neo import Graph, Node, Relationship
import pandas as pd

df = pd.read_csv('crime.csv', error_bad_lines=False, encoding='utf-8')
df = df.fillna('unknown') #填充缺失值

new = df['entity1'].str.strip()
df['entity1'] = new
new = df['entity2'].str.strip()
df['entity2'] = new
new = df['info'].str.strip()
df['info'] = new
new = df['keyword'].str.strip()
df['keyword'] = new

new = df['relation1'].str.strip()
df['relation1'] = new
new = df['relation2'].str.strip()
df['relation2'] = new
new = df['relation3'].str.strip()
df['relation3'] = new


# 连接neo4j数据库,输入地址、用户名、密码
graph = Graph("http://localhost:7474", username="neo4j", password='password')
graph.delete_all()
graph.begin()

# 创建结点
for i in range(len(df['entity1'])):
    #设置node
    node1 = Node('entity1', name=df['entity1'][i])
    graph.merge(node1, 'entity1', 'name')
    node2 = Node('entity2', name=df['entity2'][i])
    graph.merge(node2, 'entity2', 'name')
    node3 = Node('info', name=df['info'][i])
    graph.merge(node3, 'info', 'name')
    node4 = Node('keyword', name=df['keyword'][i])
    graph.merge(node4, 'keyword', 'name')

    #设置relation
    rel1 = Relationship(node1, df['relation1'][i], node2)
    rel2 = Relationship(node2, df['relation2'][i], node3)
    rel3 = Relationship(node3, df['relation3'][i], node4)
    graph.merge(rel1)
    graph.merge(rel2)
    graph.merge(rel3)

print("success")

 



这篇关于python将csv文件导入neo4j数据库中的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程