Reputation: 755
I use py2neo to connect to Neo4j.
E.g I have a query that gives this result (I can actually be anything: numbers, strings....)
number | first name | last name
--------------------------------------
1 | Anne | Hathaway
2 | Tom | Cruise
3 ....
Whenever I execute the cypher query from python (using the graph.cypher.execute or graph.cypher.stream commands), I get a Record object returned and I need to iterate through the record object to access the individual elements to access them and store them in for example a numpy matrix.
Is there a way to have the result from a cypher query stored immediately in a list,numpy matrix,.... in python?
Basically: how can I skip the record object that gets returned and avoid unnecessary manipulations to prepare the data for further calculations?
Upvotes: 0
Views: 1526
Reputation: 9369
What you get back from cypher.execute()
is a RecordList
containing Record
objects.
Both RecordList
and Record
behave like iterables, i.e. you can iterate with for x in recordlist
through all records and with for x in record
through all return values of a single record.
ResultList
has an attribute records
which returns the list of Record
objects. You can pass this to any constructor that accepts a list of lists, such as Pandas DataFrame or numpy matrix:
import py2neo
import pandas as pd
import numpy as np
result = graph.cypher.execute(myquery)
# Pandas DataFrame
df = pd.DataFrame(result.records, columns=result.columns)
# numpy matrix
matrix = np.matrix(result.records)
This works of course only if you return data types which can be stored in a DataFrame or matrix.
Upvotes: 3