-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport_to_dataframe.py
More file actions
47 lines (39 loc) · 988 Bytes
/
export_to_dataframe.py
File metadata and controls
47 lines (39 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# This example demonstrates how to run a Cypher query and export the results to a
# Pandas DataFrame.
#
# You can add the expected data to the database using the `examples/create_data.py`
# example.
import os
from dotenv import load_dotenv
load_dotenv()
from neo4j import GraphDatabase, Result
# Initialize the Neo4j driver
driver = GraphDatabase.driver(
os.getenv('NEO4J_URI'),
auth=(
os.getenv('NEO4J_USERNAME'),
os.getenv('NEO4J_PASSWORD')
)
)
# Verify the connection
driver.verify_connectivity()
# Define the query
cypher_query = """
MATCH (p:Person)-[:LIVES_IN]->(l:Location)
MATCH (p)-[w:WORKS_AT]->(c:Company)
RETURN
p.name as name,
c.name as company,
w.position as position,
l.name as location
"""
# Execute the query using the Result transformer
df = driver.execute_query(
cypher_query,
result_transformer_=Result.to_df,
location='London'
)
# Print the DataFrame
print(df)
# Close the driver
driver.close()