使用 Pandas Dataframe 显示 Cypher 查询的结果

问题描述 投票:0回答:1

我正在使用 Python 中的 Neo4j,并以这种方式在节点级别比较两个图:

query = """
  MATCH (n)-[r1]-(), (n)-[r2]-()
  WHERE r1.disease = 'MyLabel1'
  AND r2.disease = 'MyLabel2'
  RETURN DISTINCT n
"""

results = driver.execute_query(query)
results[0]
  1. 查询的结果存储在列表的第一个索引处,我想使用 Pandas Dataframe 显示结果;

  2. 我想使用 matplotlib 中的维恩图来绘制每个图中的节点数以及公共节点

pandas matplotlib neo4j cypher neo4j-python-driver
1个回答
0
投票

这个例子应该适合你:

from matplotlib_venn import venn2
import matplotlib.pyplot as plt
from neo4j import GraphDatabase

uri = "bolt://localhost:7687"
user = "neo4j"
password = "xxxxxx"

driver = GraphDatabase.driver(uri, auth=(user, password))

query = """
  MATCH (n1)-[r1]-()
  WHERE r1.disease = 'MyLabel1'
  WITH COLLECT(DISTINCT n1) AS n1s
  MATCH (n2)-[r2]-()
  WHERE r2.disease = 'MyLabel2'
  WITH n1s, COLLECT(DISTINCT n2) AS n2s
  RETURN SIZE(n1s) AS count1, SIZE(n2s) AS count2, SIZE([n IN n1s WHERE n IN n2s]) AS countIntersection
"""

count1, count2, countIntersection = driver.execute_query(query)[0][0]

venn2(subsets=(count1, count2, countIntersection), set_labels=('MyLabel1 nodes', 'MyLabel2 nodes'))
plt.show()

注意:此查询使用无向关系模式(与原始查询类似),因此同一关系两端的节点都包含在

count1
count2
中。

© www.soinside.com 2019 - 2024. All rights reserved.