python - Neo4j Bolt StatementResult to Pandas DataFrame -
based on example 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) a.name = 'arthur' return a.name name, a.title title") record in result: print("%s %s" % (record["title"], record["name"])) session.close()
here result
of datatype neo4j.v1.session.statementresult
. how access data in pandas dataframe without explicitly iterating?
pd.dataframe.from_records(result)
doesn't seem help.
this have using list comprehension
resultlist = [[record['title'], record['name']] record in result] pd.dataframe.from_records(resultlist, columns=['title', 'name'])
the best can come list comprehension similar yours, less verbose:
df = pd.dataframe([r.values() r in result], columns=result.keys())
the py2neo
package seems more suitable dataframes, it's straightforward return list of dictionaries. here's equivalent code using py2neo
:
import py2neo # of these keyword arguments unnecessary, 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) a.name = 'arthur' return a.name name, a.title title" df = pd.dataframe(graph.data(query))
Comments
Post a Comment