Python Neo4j螺栓状态结果到数据框
基于 这里的Python Neo4j螺栓状态结果到数据框,python,pandas,neo4j,Python,Pandas,Neo4j,基于 这里的result是数据类型neo4j.v1.session.StatementResult。如何在不显式迭代的情况下访问pandas dataframe中的此数据 pd.DataFrame.from_记录(结果)似乎没有帮助 这是我用列表理解法得到的 resultlist = [[record['title'], record['name']] for record in result] pd.DataFrame.from_records(resultlist, columns=['ti
result
是数据类型neo4j.v1.session.StatementResult
。如何在不显式迭代的情况下访问pandas dataframe中的此数据
pd.DataFrame.from_记录(结果)
似乎没有帮助
这是我用列表理解法得到的
resultlist = [[record['title'], record['name']] for record in result]
pd.DataFrame.from_records(resultlist, columns=['title', 'name'])
我所能想到的最好的理解是一个与您的理解类似的列表,但不太详细:
df = pd.DataFrame([r.values() for r in result], columns=result.keys())
这个包似乎更适合数据帧,因为返回字典列表非常简单。下面是使用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))
将结果记录转换到字典中可以实现以下技巧:
df = pd.DataFrame([dict(record) for record in result])
在
py2neo
的V4
中,到pandas数据帧的转换更加容易
import py2neo
# Some of these keyword arguments are unnecessary, as they are the default values.
graph = py2neo.Graph(uri, user=username, password=password)
df = graph.run("Your query goes here").to_data_frame()
那么:
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.records(), columns=result.keys())
对我来说很有用。py2neo在调用
到resultWorks上的\u data\u frame
时,除了列表理解之外,没有做更多的事情!这应该被接受为答案。非常干净,整洁,像蟒蛇一样。
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.records(), columns=result.keys())