Joe*_*oel 2 python neo4j pandas
基于Neo4j的例子
from neo4j.v1 import GraphDatabase, basic_auth
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()
session.run("CREATE (a:Person {name:'Arthur', title:'King'})")
result = session.run("MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title")
for record in result:
print("%s %s" % (record["title"], record["name"]))
session.close()
Run Code Online (Sandbox Code Playgroud)
这result是数据类型neo4j.v1.session.StatementResult.如何在没有显式迭代的情况下访问pandas数据帧中的这些数据?
pd.DataFrame.from_records(result) 似乎没有帮助.
这就是我使用列表理解的方法
resultlist = [[record['title'], record['name']] for record in result]
pd.DataFrame.from_records(resultlist, columns=['title', 'name'])
Run Code Online (Sandbox Code Playgroud)
小智 6
将结果记录转换为字典可以解决问题:
df = pd.DataFrame([dict(record) for record in result])
Run Code Online (Sandbox Code Playgroud)
我能想出的最好的是与你的类似的列表理解,但不那么冗长:
df = pd.DataFrame([r.values() for r in result], columns=result.keys())
Run Code Online (Sandbox Code Playgroud)
该py2neo软件包似乎更适合DataFrames,因为返回字典列表相当简单.这是使用的等效代码py2neo:
import py2neo
# Some of these keyword arguments are unnecessary, as they are the default values.
graph = py2neo.Graph(bolt=True, host='localhost', user='neo4j', password='neo4j')
graph.run("CREATE (a:Person {name:'Arthur', title:'King'})")
query = "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title"
df = pd.DataFrame(graph.data(query))
Run Code Online (Sandbox Code Playgroud)