Py2Neo - 如何绕过迭代从密码查询返回的记录对象以访问查询数据
Py2Neo - how to get around iterating the record object returned from cypher queries to access the query data
我使用 py2neo 连接到 Neo4j。
例如,我有一个查询给出了这个结果(实际上我可以是任何东西:数字、字符串....)
number | first name | last name
--------------------------------------
1 | Anne | Hathaway
2 | Tom | Cruise
3 ....
每当我从 python 执行密码查询时(使用 graph.cypher.execute 或 graph.cypher.stream 命令),我都会返回一个 Record 对象,我需要遍历记录对象以访问各个元素以访问它们并将它们存储在例如 numpy 矩阵中。
有没有办法将密码查询的结果立即存储在列表、numpy 矩阵...中 python?
基本上:如何跳过返回的记录对象并避免不必要的操作来为进一步计算准备数据?
您从 cypher.execute()
返回的是包含 Record
个对象的 RecordList
。
RecordList
和 Record
的行为都像可迭代对象,即您可以使用 for x in recordlist
遍历所有记录,使用 for x in record
遍历所有 return 值一条记录。
ResultList
有一个属性 records
,它 return 是 Record
对象的列表。您可以将其传递给任何接受列表列表的构造函数,例如 Pandas DataFrame 或 numpy 矩阵:
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)
这当然只有当您 return 可以存储在 DataFrame 或矩阵中的数据类型时才有效。
我使用 py2neo 连接到 Neo4j。
例如,我有一个查询给出了这个结果(实际上我可以是任何东西:数字、字符串....)
number | first name | last name
--------------------------------------
1 | Anne | Hathaway
2 | Tom | Cruise
3 ....
每当我从 python 执行密码查询时(使用 graph.cypher.execute 或 graph.cypher.stream 命令),我都会返回一个 Record 对象,我需要遍历记录对象以访问各个元素以访问它们并将它们存储在例如 numpy 矩阵中。
有没有办法将密码查询的结果立即存储在列表、numpy 矩阵...中 python?
基本上:如何跳过返回的记录对象并避免不必要的操作来为进一步计算准备数据?
您从 cypher.execute()
返回的是包含 Record
个对象的 RecordList
。
RecordList
和 Record
的行为都像可迭代对象,即您可以使用 for x in recordlist
遍历所有记录,使用 for x in record
遍历所有 return 值一条记录。
ResultList
有一个属性 records
,它 return 是 Record
对象的列表。您可以将其传递给任何接受列表列表的构造函数,例如 Pandas DataFrame 或 numpy 矩阵:
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)
这当然只有当您 return 可以存储在 DataFrame 或矩阵中的数据类型时才有效。