Skip to content

Instantly share code, notes, and snippets.

@egemenzeytinci
Last active November 10, 2020 22:56
Show Gist options
  • Save egemenzeytinci/76efcc422388af7a1f5984b182999ceb to your computer and use it in GitHub Desktop.
Save egemenzeytinci/76efcc422388af7a1f5984b182999ceb to your computer and use it in GitHub Desktop.
from py2neo import Graph
import pandas as pd
host = 'localhost'
port = 7687
user = ''
password = ''
graph = Graph(
host=host,
port=port,
user=user,
password=password
)
tx = graph.begin()
query = """
MATCH (c:Customer)-[r1:MADE]->(t:Transaction)-[r2:CONTAINS]->(:Product)
WITH SUM(r2.price) AS monetary,
COUNT(DISTINCT t) AS frequency,
c.customerID AS customer,
MIN(
duration.inDays(
date(datetime({epochmillis: apoc.date.parse(t.transactionDate, 'ms', 'MM/dd/yyyy')})),
date()
).days
) AS recency
RETURN customer, recency, frequency, monetary
"""
# create the dataframe
results = tx.run(query).data()
df = pd.DataFrame(results)
# edit the recency value
df['recency'] = df['recency'] - df['recency'].min()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment