Neo4j Bolt StatementResult to Pandas DataFrame

时间:2016-07-25 20:16:49

标签: python pandas neo4j

基于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()

此处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'])

4 个答案:

答案 0 :(得分:5)

我能想到的最好的是与你的类似的列表理解,但不那么冗长:

df = pd.DataFrame([r.values() for r in result], columns=result.keys())

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

答案 1 :(得分:2)

将结果记录转换为字典可以解决问题:

df = pd.DataFrame([dict(record) for record in result])

答案 2 :(得分:1)

那又怎么样:

from neo4j.v1 import GraphDatabase
from pandas import DataFrame

uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("",""))

get_instances = """MATCH (n)--(m)
                   RETURN n
                   LIMIT 10
                   """

with driver.session() as graphDB_Session:
    result = graphDB_Session.run(get_instances)
    df = DataFrame(result, columns=result.keys())

为我工作。

答案 3 :(得分:0)

V4的{​​{1}}中,转换为pandas DataFrame更加容易。

py2neo