numpy check two array on multiple column

Multi tool use
Multi tool use











up vote
0
down vote

favorite












Lets say edata and tdata two 2D numpy array with shape (x,6) and (y,6) where x and y are two arbitrary number and must not be equal. Now I have to check if there is any row in edata such that both 2nd and 3rd column value of that row equals to that of some row of tdata. If there is any such row then save them in another. For now I have written the following code. But I think numpy has better way to that instead of explicitly iterating and checking for values. Can you suggest me more efficient way?



res = np.array(, dtype=np.float64).reshape(0,6)
for line in edata:
ind = line[1] == tdata[:,1]
ind = line[2] == tdata[ind,2]
if np.any(ind):
res = np.vstack((res,line))









share|improve this question


























    up vote
    0
    down vote

    favorite












    Lets say edata and tdata two 2D numpy array with shape (x,6) and (y,6) where x and y are two arbitrary number and must not be equal. Now I have to check if there is any row in edata such that both 2nd and 3rd column value of that row equals to that of some row of tdata. If there is any such row then save them in another. For now I have written the following code. But I think numpy has better way to that instead of explicitly iterating and checking for values. Can you suggest me more efficient way?



    res = np.array(, dtype=np.float64).reshape(0,6)
    for line in edata:
    ind = line[1] == tdata[:,1]
    ind = line[2] == tdata[ind,2]
    if np.any(ind):
    res = np.vstack((res,line))









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Lets say edata and tdata two 2D numpy array with shape (x,6) and (y,6) where x and y are two arbitrary number and must not be equal. Now I have to check if there is any row in edata such that both 2nd and 3rd column value of that row equals to that of some row of tdata. If there is any such row then save them in another. For now I have written the following code. But I think numpy has better way to that instead of explicitly iterating and checking for values. Can you suggest me more efficient way?



      res = np.array(, dtype=np.float64).reshape(0,6)
      for line in edata:
      ind = line[1] == tdata[:,1]
      ind = line[2] == tdata[ind,2]
      if np.any(ind):
      res = np.vstack((res,line))









      share|improve this question













      Lets say edata and tdata two 2D numpy array with shape (x,6) and (y,6) where x and y are two arbitrary number and must not be equal. Now I have to check if there is any row in edata such that both 2nd and 3rd column value of that row equals to that of some row of tdata. If there is any such row then save them in another. For now I have written the following code. But I think numpy has better way to that instead of explicitly iterating and checking for values. Can you suggest me more efficient way?



      res = np.array(, dtype=np.float64).reshape(0,6)
      for line in edata:
      ind = line[1] == tdata[:,1]
      ind = line[2] == tdata[ind,2]
      if np.any(ind):
      res = np.vstack((res,line))






      python numpy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 14:54









      Eular

      4731724




      4731724
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          You can use equal and outer to see if any value in a column of edata is in a column of tdata. You check for both the 2nd and 3rd columns, then use any on axis=1 to get edata rows as required such as:



          res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
          &np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]


          for example, with simple input:



          edata = np.arange(4*6).reshape(4,6)
          tdata = np.arange(6*6).reshape(6,6) + 12

          print (res)
          array([[12, 13, 14, 15, 16, 17],
          [18, 19, 20, 21, 22, 23]])


          which are the last two rows of edata that have the same value in column 2 and 3 than the first two rows of tdata






          share|improve this answer





















          • check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
            – Eular
            Nov 22 at 19:00










          • @Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
            – Ben.T
            Nov 22 at 19:23












          • Sry, i was checking with 1st and 2nd column, my bad
            – Eular
            Nov 22 at 19:44










          • @Eular no pb :)
            – Ben.T
            Nov 22 at 19:59











          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',
          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%2f53433563%2fnumpy-check-two-array-on-multiple-column%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          You can use equal and outer to see if any value in a column of edata is in a column of tdata. You check for both the 2nd and 3rd columns, then use any on axis=1 to get edata rows as required such as:



          res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
          &np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]


          for example, with simple input:



          edata = np.arange(4*6).reshape(4,6)
          tdata = np.arange(6*6).reshape(6,6) + 12

          print (res)
          array([[12, 13, 14, 15, 16, 17],
          [18, 19, 20, 21, 22, 23]])


          which are the last two rows of edata that have the same value in column 2 and 3 than the first two rows of tdata






          share|improve this answer





















          • check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
            – Eular
            Nov 22 at 19:00










          • @Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
            – Ben.T
            Nov 22 at 19:23












          • Sry, i was checking with 1st and 2nd column, my bad
            – Eular
            Nov 22 at 19:44










          • @Eular no pb :)
            – Ben.T
            Nov 22 at 19:59















          up vote
          1
          down vote













          You can use equal and outer to see if any value in a column of edata is in a column of tdata. You check for both the 2nd and 3rd columns, then use any on axis=1 to get edata rows as required such as:



          res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
          &np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]


          for example, with simple input:



          edata = np.arange(4*6).reshape(4,6)
          tdata = np.arange(6*6).reshape(6,6) + 12

          print (res)
          array([[12, 13, 14, 15, 16, 17],
          [18, 19, 20, 21, 22, 23]])


          which are the last two rows of edata that have the same value in column 2 and 3 than the first two rows of tdata






          share|improve this answer





















          • check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
            – Eular
            Nov 22 at 19:00










          • @Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
            – Ben.T
            Nov 22 at 19:23












          • Sry, i was checking with 1st and 2nd column, my bad
            – Eular
            Nov 22 at 19:44










          • @Eular no pb :)
            – Ben.T
            Nov 22 at 19:59













          up vote
          1
          down vote










          up vote
          1
          down vote









          You can use equal and outer to see if any value in a column of edata is in a column of tdata. You check for both the 2nd and 3rd columns, then use any on axis=1 to get edata rows as required such as:



          res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
          &np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]


          for example, with simple input:



          edata = np.arange(4*6).reshape(4,6)
          tdata = np.arange(6*6).reshape(6,6) + 12

          print (res)
          array([[12, 13, 14, 15, 16, 17],
          [18, 19, 20, 21, 22, 23]])


          which are the last two rows of edata that have the same value in column 2 and 3 than the first two rows of tdata






          share|improve this answer












          You can use equal and outer to see if any value in a column of edata is in a column of tdata. You check for both the 2nd and 3rd columns, then use any on axis=1 to get edata rows as required such as:



          res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
          &np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]


          for example, with simple input:



          edata = np.arange(4*6).reshape(4,6)
          tdata = np.arange(6*6).reshape(6,6) + 12

          print (res)
          array([[12, 13, 14, 15, 16, 17],
          [18, 19, 20, 21, 22, 23]])


          which are the last two rows of edata that have the same value in column 2 and 3 than the first two rows of tdata







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 15:37









          Ben.T

          5,2872523




          5,2872523












          • check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
            – Eular
            Nov 22 at 19:00










          • @Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
            – Ben.T
            Nov 22 at 19:23












          • Sry, i was checking with 1st and 2nd column, my bad
            – Eular
            Nov 22 at 19:44










          • @Eular no pb :)
            – Ben.T
            Nov 22 at 19:59


















          • check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
            – Eular
            Nov 22 at 19:00










          • @Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
            – Ben.T
            Nov 22 at 19:23












          • Sry, i was checking with 1st and 2nd column, my bad
            – Eular
            Nov 22 at 19:44










          • @Eular no pb :)
            – Ben.T
            Nov 22 at 19:59
















          check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
          – Eular
          Nov 22 at 19:00




          check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
          – Eular
          Nov 22 at 19:00












          @Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
          – Ben.T
          Nov 22 at 19:23






          @Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
          – Ben.T
          Nov 22 at 19:23














          Sry, i was checking with 1st and 2nd column, my bad
          – Eular
          Nov 22 at 19:44




          Sry, i was checking with 1st and 2nd column, my bad
          – Eular
          Nov 22 at 19:44












          @Eular no pb :)
          – Ben.T
          Nov 22 at 19:59




          @Eular no pb :)
          – Ben.T
          Nov 22 at 19:59


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53433563%2fnumpy-check-two-array-on-multiple-column%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







          RZoziVrc0ov0Ig Anr,7gO P0Rh2iXw5a,dy
          jDG2riDzAwZ9sxyRcsDM,M7Ifiy7LK0gthIQ4v

          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu