Last active
November 10, 2020 22:56
-
-
Save egemenzeytinci/76efcc422388af7a1f5984b182999ceb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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