目录
[1.1 获取数据集](#1.1 获取数据集)
[1.2 以"记事本"方式打开文件](#1.2 以“记事本”方式打开文件)
[1.3 另存为"UTF-8"格式文件](#1.3 另存为“UTF-8”格式文件)
[1.4 选择"是"](#1.4 选择“是”)
[二、 打开Neo4j并运行](#二、 打开Neo4j并运行)
[2.1 创建新的Neo4j数据库](#2.1 创建新的Neo4j数据库)
[2.2 分别设置数据库名和密码](#2.2 分别设置数据库名和密码)
[编辑 2.3 启动Neo4j数据库](#编辑 2.3 启动Neo4j数据库)
[2.4 打开Neo4j数据库](#2.4 打开Neo4j数据库)
[2.5 运行查看该数据库是否为空](#2.5 运行查看该数据库是否为空)
[3.1 创建一个包,存项目](#3.1 创建一个包,存项目)
[3.2 创建一个项目](#3.2 创建一个项目)
[3.3 检查自己的依赖是否完全](#3.3 检查自己的依赖是否完全)
[3.3.1 点击"File"-"Settings"](#3.3.1 点击“File”-“Settings”)
[3.3.2 找到"Project:PythonProject"-"Python Interpreter"](#3.3.2 找到“Project:PythonProject”-"Python Interpreter")
[3.4 导入相关依赖,打开Terminal](#3.4 导入相关依赖,打开Terminal)
[3.5 输入命令](#3.5 输入命令)
[4.1 Python连接Neo4j测试](#4.1 Python连接Neo4j测试)
[4.2 导入数据集](#4.2 导入数据集)
[4.3 处理数据集](#4.3 处理数据集)
[4.4 初始化neo4j并合并数据集关系](#4.4 初始化neo4j并合并数据集关系)
[4.5 把节点导入到Neo4j中](#4.5 把节点导入到Neo4j中)
[4.6 运行代码后,查看Neo4j是否有节点](#4.6 运行代码后,查看Neo4j是否有节点)
[4.7 导入节点关系](#4.7 导入节点关系)
[4.8 查看Neo4j](#4.8 查看Neo4j)
[5.1 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte](#5.1 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte)
一、获取数据集
1.1 获取数据集
《我是刑警》的人物关系数据集资源https://download.csdn.net/download/Z0412_J0103/90160496

1.2 以"记事本"方式打开文件

1.3 另存为"UTF-8"格式文件

1.4 选择"是"

二、 打开Neo4j并运行
2.1 创建新的Neo4j数据库

2.2 分别设置数据库名和密码
2.3 启动Neo4j数据库

2.4 打开Neo4j数据库

2.5 运行查看该数据库是否为空
python
match(n) return n;


三、打开Python创建项目
3.1 创建一个包,存项目

3.2 创建一个项目

3.3 检查自己的依赖是否完全
3.3.1 点击"File"-"Settings"

3.3.2 找到"Project:PythonProject"-"Python Interpreter"
检查是否导入了"neo4j"、"pandas"、"py2neo"

3.4 导入相关依赖,打开Terminal

3.5 输入命令
python
pip install neo4j-python-driver pandas py2neo

四、实现Python连接Neo4j
4.1 Python连接Neo4j测试
pythonfrom py2neo import * import pandas as pd from neo4j import GraphDatabase graph = Graph('http://neo4j:123456@127.0.0.1:7474')
其中的"123456"是你刚刚建立的Neo4j密码,此处只需要修改该密码就可以了

4.2 导入数据集
pythoninvoice_data = pd.read_csv("D:/我是刑警.csv", header=0) print(invoice_data.shape)
其中地址需要改变,其它地方都不用变
运行结果如下

4.3 处理数据集
pythonpeoples = list(set(invoice_data['人物'].astype(str))) people_ties = list(set(invoice_data['关系人物'].astype(str))) people = list(set(peoples).union(set(people_ties))) print(people) # 清除neo4j里面的所有数据 graph.delete_all()
执行结果如下

4.4 初始化neo4j并合并数据集关系
python
# 初始化一个空列表来存储所有的元组
tuple_total = []
# 迭代DataFrame的每一行
for index, row in invoice_data.iterrows():
ties_total = [row['人物'], row['关系'], row['关系人物']]
# 将所有元组添加到结果列表中
tuple_total.extend([ties_total])
print(tuple_total)

4.5 把节点导入到Neo4j中
python
#把节点导入neo4j中
def create_node(people):
for name in people:
node_1 = Node('人物', name = name)
graph.create(node_1)
create_node(people)

4.6 运行代码后,查看Neo4j是否有节点
4.7 导入节点关系
python
matcher = NodeMatcher(graph)
# 导入关系
for i in range(0, len(tuple_total)):
name_1 = matcher.match('人物', name=tuple_total[i][0]).first()
name_2 = matcher.match('人物', name=tuple_total[i][2]).first()
rel = tuple_total[i][1]
relationship = Relationship(name_1, rel, name_2)
graph.create(relationship)

4.8 查看Neo4j
五、该过程出现的Bug
5.1 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte

解决过程:
是因为你的CSV文件没有设置为"UTF-8" ,具体过程看本文的1.2和1.3过程

上一篇文章:CSDN中群发功能及自动回复设置教程_csdn粉丝群发https://blog.csdn.net/Z0412_J0103/article/details/144074981下一篇文章: