Exporting Results
Tutorial
Exporting Results
Export query results to CSV, Parquet, DataFrame, and NetworkX
What You'll Learn
- Export to CSV - Save query results as comma-separated files
- Export to Parquet - Columnar format for efficient analytics
- Export to DataFrame - Query directly into Polars DataFrames
- Export to NetworkX - Convert results to a Python graph for analysis
# Cell 1 — ParametersUSERNAME = "_FILL_ME_IN_" # Set your email before running# Cell 2 — Connectfrom graph_olap import GraphOLAPClientclient = GraphOLAPClient(username=USERNAME)
# Cell 3 — Provisionfrom notebook_setup import provisionpersonas, conn = provision(USERNAME)analyst = personas["analyst"]admin = personas["admin"]ops = personas["ops"]client = analyst
print(f"Connected | {conn.query_scalar('MATCH (n) RETURN count(n)')} nodes")
1
Export to CSV
Save query results as a CSV file
# Run a query and export results to CSVresult = conn.query("MATCH (c:Customer) RETURN c.id LIMIT 10")result.to_csv("customers.csv")print("Exported to customers.csv")
# Preview what was writtenimport pathlibprint(pathlib.Path("customers.csv").read_text()[:500])
2
Export to Parquet
Columnar format for analytics workloads
# Parquet is ideal for large exports — smaller files, faster readsresult = conn.query(""" MATCH (a:Customer)-[:SHARES_ACCOUNT]->(b:Customer) RETURN a.id AS from_customer, b.id AS to_customer LIMIT 20""")result.to_parquet("customer_accounts.parquet")print("Exported to customer_accounts.parquet")
import ossize_kb = os.path.getsize("customer_accounts.parquet") / 1024print(f"File size: {size_kb:.1f} KB")
3
Export to DataFrame
Query directly into a Polars DataFrame
# query_df() returns a Polars DataFrame directly — no intermediate stepdf = conn.query_df(""" MATCH (c:Customer) RETURN c.id AS name LIMIT 10""")df
4
Export to NetworkX
Convert results to a Python graph for analysis
# Convert query results to a NetworkX graph for Python-native analysisresult = conn.query(""" MATCH (a:Customer)-[r:SHARES_ACCOUNT]->(b:Customer) RETURN a, r, b LIMIT 50""")
G = result.to_networkx()print(f"NetworkX graph: {G.number_of_nodes()} nodes, {G.number_of_edges()} edges")print(f"Node types: {set(dict(G.nodes(data='label', default='unknown')).values())}")Key Takeaways
- Use
result.to_csv(path)for simple, portable exports - Use
result.to_parquet(path)for compact, columnar storage - Use
conn.query_df(cypher)for direct Polars DataFrame access - Use
result.to_networkx()to build a Python graph for algorithms and visualization - All exports work on query results — there is no separate export resource