![](/img/trans.png)
[英]Create nodes and relationships in Neo4j with inputs as a pandas DataFrame with py2neo
[英]Creating relationships of nodes of the same kind in Neo4j Cypher
這是我使用的數據幀的一部分:
A_PERSON, B_PERSON, MESSAGE_TIME
SARAH, RON, 13:20 pm
RON, TIM, 13:33 pm
TIM, MARTHA, 13:45 pm
RON, LARRY, 13:35 pm
LARRY, JIM, 13:38 pm
我想創建一個圖表來顯示這些人之間發送的所有消息,箭頭應該顯示消息的時間。
我試過這樣的事情:
load csv with headers
from "file:\\df.csv" as row
create(m: Message {a_person: row.A_PERSON, b_person: row.B_PERSON, time: row.MESSAGE_TIME})
這樣以后我就可以匹配它們,但這似乎不可能。 由於第二個和第三個標簽,結果中有一些 A_PERSON 重復。 所以我不明白如何創建獨特的節點並將它們相互連接。 有可能嗎? 如果是,那又如何?
關鍵是使用MERGE
子句。 MERGE
每個人,然后MERGE
兩個人之間的關系。
如果他們不存在, MERGE
將創建該人,如果他們存在,則MATCH
他們。
LOAD CSV WITH HEADERS
FROM "file:\\df.csv" AS row
MERGE (a_person:Person {name: row.A_PERSON})
MERGE (b_person:Person {name: row.B_PERSON})
MERGE (a_person)-[:SENT_MESSAGE {time: row.MESSAGE_TIME}]->(b_person)
或者,如果您將 message 設置為節點,則可以執行以下操作。
LOAD CSV WITH HEADERS
FROM "file:\\df.csv" AS row
MERGE (a_person:Person {name: row.A_PERSON})
MERGE (b_person:Person {name: row.B_PERSON})
MERGE (a_person)-[:SENT_MESSAGE]->(:Message {time: row.MESSAGE_TIME})-[:TO]->(b_person)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.