cypher query in py2neo to update n Nodes with a list of n ids and parameters












1















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.










share|improve this question





























    1















    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.










    share|improve this question



























      1












      1








      1








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 7:35







      HerrIvan

















      asked Nov 27 '18 at 10:19









      HerrIvanHerrIvan

      1258




      1258
























          3 Answers
          3






          active

          oldest

          votes


















          1














          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





          share|improve this answer































            1














            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.






            share|improve this answer
























            • 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



















            0














            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






            share|improve this answer























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              1














              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





              share|improve this answer




























                1














                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





                share|improve this answer


























                  1












                  1








                  1







                  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





                  share|improve this answer













                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 28 '18 at 8:25









                  InverseFalconInverseFalcon

                  19.4k31831




                  19.4k31831

























                      1














                      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.






                      share|improve this answer
























                      • 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
















                      1














                      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.






                      share|improve this answer
























                      • 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














                      1












                      1








                      1







                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      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



















                      • 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











                      0














                      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






                      share|improve this answer




























                        0














                        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






                        share|improve this answer


























                          0












                          0








                          0







                          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






                          share|improve this answer













                          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







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 28 '18 at 12:25









                          HerrIvanHerrIvan

                          1258




                          1258






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                              Calculate evaluation metrics using cross_val_predict sklearn

                              Insert data from modal to MySQL (multiple modal on website)