Using result transfomers

You are working within a Jupyter notebook and want to transform the result of a query into a pandas DataFrame.

Transform the result

Select the correct method to transform the result of a query into a pandas DataFrame.

python
from neo4j import Result
driver = GraphDatabase.driver(NEO4J_URI,
    auth=(NEO4J_USER, NEO4J_PASSWORD)
)

cypher = """
MATCH (m:Movie {title: $title})<-[:ACTED_IN]-(p)
RETURN p.name AS actor
"""
params = {"title": "Toy Story"}

res = driver.execute_query(
    query, params,
    result_transformer_=Result.#select:to_df
)
  • ❏ as_df

  • ❏ to_dataframe

  • ✓ to_df

  • ❏ to_pandas

Hint

The Result class provides a method to transform results into a pandas DataFrame. The method name follows the common Python convention of using "to_" followed by the abbreviated target format.

Solution

The correct answer is to_df. This method transforms the query results into a pandas DataFrame.

python
res = driver.execute_query(
    query, params,
    result_transformer_=Result.to_df
)

Lesson summary

Congratulations! You learned how to transform the results into a DataFrame using Result.to_df().

You now have the tools needed to run simple Cypher statements and transform the results where needed.

In the next module, you will explore how to execute more complex Cypher statements.