cypher query in py2neo to update n Nodes with a list of n ids and parameters
Say I want to update a sizeable amount of already existing nodes using data that, for instance, is stored in a pd.Dataframe
. Since I know how to write a parametrized query that will handle a single node update, my basic solution is to set this query in a loop and run it for each row in the data frame.
for _,row in df.iterrows():
query='''MATCH (p:Person)
WHERE p.name={name} AND p.surname = {surname}
SET p.description={description} '''
tx.run(query,name=row['name'],surname=row['surname'],
description=row['description'])
However, there must be a more direct (and faster) way of passing this information to the query, so that the iteration is "managed" at the server side. Is that true? I haven't been able to find any documentation for that.
neo4j cypher py2neo
add a comment |
Say I want to update a sizeable amount of already existing nodes using data that, for instance, is stored in a pd.Dataframe
. Since I know how to write a parametrized query that will handle a single node update, my basic solution is to set this query in a loop and run it for each row in the data frame.
for _,row in df.iterrows():
query='''MATCH (p:Person)
WHERE p.name={name} AND p.surname = {surname}
SET p.description={description} '''
tx.run(query,name=row['name'],surname=row['surname'],
description=row['description'])
However, there must be a more direct (and faster) way of passing this information to the query, so that the iteration is "managed" at the server side. Is that true? I haven't been able to find any documentation for that.
neo4j cypher py2neo
add a comment |
Say I want to update a sizeable amount of already existing nodes using data that, for instance, is stored in a pd.Dataframe
. Since I know how to write a parametrized query that will handle a single node update, my basic solution is to set this query in a loop and run it for each row in the data frame.
for _,row in df.iterrows():
query='''MATCH (p:Person)
WHERE p.name={name} AND p.surname = {surname}
SET p.description={description} '''
tx.run(query,name=row['name'],surname=row['surname'],
description=row['description'])
However, there must be a more direct (and faster) way of passing this information to the query, so that the iteration is "managed" at the server side. Is that true? I haven't been able to find any documentation for that.
neo4j cypher py2neo
Say I want to update a sizeable amount of already existing nodes using data that, for instance, is stored in a pd.Dataframe
. Since I know how to write a parametrized query that will handle a single node update, my basic solution is to set this query in a loop and run it for each row in the data frame.
for _,row in df.iterrows():
query='''MATCH (p:Person)
WHERE p.name={name} AND p.surname = {surname}
SET p.description={description} '''
tx.run(query,name=row['name'],surname=row['surname'],
description=row['description'])
However, there must be a more direct (and faster) way of passing this information to the query, so that the iteration is "managed" at the server side. Is that true? I haven't been able to find any documentation for that.
neo4j cypher py2neo
neo4j cypher py2neo
edited Nov 28 '18 at 7:35
HerrIvan
asked Nov 27 '18 at 10:19
HerrIvanHerrIvan
1258
1258
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Instead of looping like this, with one Cypher query executed per entry, you should gather all that into a list parameter of map objects and make a single Cypher query (you could batch this though if you have > 100k or so entries to process). Michael Hunger has a good blog entry on this approach.
You can use UNWIND on the list parameter to transform it into rows, and handle everything all at once. Assuming you pass in the list as data
:
UNWIND $data as row
MATCH (p:Person)
WHERE p.name = row.name AND p.surname = row.surname
SET p.description = row.description
add a comment |
You can do that by runnin a cypher LOAD query and providing a csv file containing your data:
LOAD CSV WITH HEADERS FROM 'file:///file.csv' as csvLine fieldterminator ';'
MATCH (p:Person {name:csvLine.name, p.surname:csvLine.surname})
SET p.description=csvLine.description
But I don't think there is a solution to pass array of data to a match loop.
It feels like a bit of a hack... But thanks anyway. Let's see if something better comes.
– HerrIvan
Nov 27 '18 at 13:54
add a comment |
The essential problem is already addressed in the answer by InverseFalcon, but to provide a complete answer including the py2neo
and pandas bits
, I post the code below:
query='''UNWIND {batch} AS row
MATCH (p:Person)
WHERE p.name=row.name AND p.surname = row.surname
SET p.description=row.description '''
graph.run(query,batch=df.to_dict(orient='records'))
So, at the end this was more of a neo4j
than a py2neo
question, and the relevant piece of info in neo4j's docs is here
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53497425%2fcypher-query-in-py2neo-to-update-n-nodes-with-a-list-of-n-ids-and-parameters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of looping like this, with one Cypher query executed per entry, you should gather all that into a list parameter of map objects and make a single Cypher query (you could batch this though if you have > 100k or so entries to process). Michael Hunger has a good blog entry on this approach.
You can use UNWIND on the list parameter to transform it into rows, and handle everything all at once. Assuming you pass in the list as data
:
UNWIND $data as row
MATCH (p:Person)
WHERE p.name = row.name AND p.surname = row.surname
SET p.description = row.description
add a comment |
Instead of looping like this, with one Cypher query executed per entry, you should gather all that into a list parameter of map objects and make a single Cypher query (you could batch this though if you have > 100k or so entries to process). Michael Hunger has a good blog entry on this approach.
You can use UNWIND on the list parameter to transform it into rows, and handle everything all at once. Assuming you pass in the list as data
:
UNWIND $data as row
MATCH (p:Person)
WHERE p.name = row.name AND p.surname = row.surname
SET p.description = row.description
add a comment |
Instead of looping like this, with one Cypher query executed per entry, you should gather all that into a list parameter of map objects and make a single Cypher query (you could batch this though if you have > 100k or so entries to process). Michael Hunger has a good blog entry on this approach.
You can use UNWIND on the list parameter to transform it into rows, and handle everything all at once. Assuming you pass in the list as data
:
UNWIND $data as row
MATCH (p:Person)
WHERE p.name = row.name AND p.surname = row.surname
SET p.description = row.description
Instead of looping like this, with one Cypher query executed per entry, you should gather all that into a list parameter of map objects and make a single Cypher query (you could batch this though if you have > 100k or so entries to process). Michael Hunger has a good blog entry on this approach.
You can use UNWIND on the list parameter to transform it into rows, and handle everything all at once. Assuming you pass in the list as data
:
UNWIND $data as row
MATCH (p:Person)
WHERE p.name = row.name AND p.surname = row.surname
SET p.description = row.description
answered Nov 28 '18 at 8:25
InverseFalconInverseFalcon
19.4k31831
19.4k31831
add a comment |
add a comment |
You can do that by runnin a cypher LOAD query and providing a csv file containing your data:
LOAD CSV WITH HEADERS FROM 'file:///file.csv' as csvLine fieldterminator ';'
MATCH (p:Person {name:csvLine.name, p.surname:csvLine.surname})
SET p.description=csvLine.description
But I don't think there is a solution to pass array of data to a match loop.
It feels like a bit of a hack... But thanks anyway. Let's see if something better comes.
– HerrIvan
Nov 27 '18 at 13:54
add a comment |
You can do that by runnin a cypher LOAD query and providing a csv file containing your data:
LOAD CSV WITH HEADERS FROM 'file:///file.csv' as csvLine fieldterminator ';'
MATCH (p:Person {name:csvLine.name, p.surname:csvLine.surname})
SET p.description=csvLine.description
But I don't think there is a solution to pass array of data to a match loop.
It feels like a bit of a hack... But thanks anyway. Let's see if something better comes.
– HerrIvan
Nov 27 '18 at 13:54
add a comment |
You can do that by runnin a cypher LOAD query and providing a csv file containing your data:
LOAD CSV WITH HEADERS FROM 'file:///file.csv' as csvLine fieldterminator ';'
MATCH (p:Person {name:csvLine.name, p.surname:csvLine.surname})
SET p.description=csvLine.description
But I don't think there is a solution to pass array of data to a match loop.
You can do that by runnin a cypher LOAD query and providing a csv file containing your data:
LOAD CSV WITH HEADERS FROM 'file:///file.csv' as csvLine fieldterminator ';'
MATCH (p:Person {name:csvLine.name, p.surname:csvLine.surname})
SET p.description=csvLine.description
But I don't think there is a solution to pass array of data to a match loop.
answered Nov 27 '18 at 13:30
MuldecMuldec
6239
6239
It feels like a bit of a hack... But thanks anyway. Let's see if something better comes.
– HerrIvan
Nov 27 '18 at 13:54
add a comment |
It feels like a bit of a hack... But thanks anyway. Let's see if something better comes.
– HerrIvan
Nov 27 '18 at 13:54
It feels like a bit of a hack... But thanks anyway. Let's see if something better comes.
– HerrIvan
Nov 27 '18 at 13:54
It feels like a bit of a hack... But thanks anyway. Let's see if something better comes.
– HerrIvan
Nov 27 '18 at 13:54
add a comment |
The essential problem is already addressed in the answer by InverseFalcon, but to provide a complete answer including the py2neo
and pandas bits
, I post the code below:
query='''UNWIND {batch} AS row
MATCH (p:Person)
WHERE p.name=row.name AND p.surname = row.surname
SET p.description=row.description '''
graph.run(query,batch=df.to_dict(orient='records'))
So, at the end this was more of a neo4j
than a py2neo
question, and the relevant piece of info in neo4j's docs is here
add a comment |
The essential problem is already addressed in the answer by InverseFalcon, but to provide a complete answer including the py2neo
and pandas bits
, I post the code below:
query='''UNWIND {batch} AS row
MATCH (p:Person)
WHERE p.name=row.name AND p.surname = row.surname
SET p.description=row.description '''
graph.run(query,batch=df.to_dict(orient='records'))
So, at the end this was more of a neo4j
than a py2neo
question, and the relevant piece of info in neo4j's docs is here
add a comment |
The essential problem is already addressed in the answer by InverseFalcon, but to provide a complete answer including the py2neo
and pandas bits
, I post the code below:
query='''UNWIND {batch} AS row
MATCH (p:Person)
WHERE p.name=row.name AND p.surname = row.surname
SET p.description=row.description '''
graph.run(query,batch=df.to_dict(orient='records'))
So, at the end this was more of a neo4j
than a py2neo
question, and the relevant piece of info in neo4j's docs is here
The essential problem is already addressed in the answer by InverseFalcon, but to provide a complete answer including the py2neo
and pandas bits
, I post the code below:
query='''UNWIND {batch} AS row
MATCH (p:Person)
WHERE p.name=row.name AND p.surname = row.surname
SET p.description=row.description '''
graph.run(query,batch=df.to_dict(orient='records'))
So, at the end this was more of a neo4j
than a py2neo
question, and the relevant piece of info in neo4j's docs is here
answered Nov 28 '18 at 12:25
HerrIvanHerrIvan
1258
1258
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53497425%2fcypher-query-in-py2neo-to-update-n-nodes-with-a-list-of-n-ids-and-parameters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown