Creating new matrix based on dictionary values and a list











up vote
0
down vote

favorite












The following is a simplified example:



val = [10,23,45,31,78,43,1,67,82]

indx = [1,4,5,8]
indx2 = [3,6,7]
indx3 = [0,2]

samp = {}
samp[0] = indx
samp[1] = indx2
samp[2] = indx3


Say I have a dictionary (samp) that has two groups: Group 0 and Group 1.
The dictionary has indicies for values in the vector val.



I want to pull out all of the values in val based on the given group in the dictionary by creating a 8 X 2 matrix,
Where i have the group and the value in two columns in order by index so it looks like this:



val  group
10 2
23 0
45 2
31 0
87 0
43 1
1 1
67 0
82 1


How do I go about doing this?










share|improve this question




























    up vote
    0
    down vote

    favorite












    The following is a simplified example:



    val = [10,23,45,31,78,43,1,67,82]

    indx = [1,4,5,8]
    indx2 = [3,6,7]
    indx3 = [0,2]

    samp = {}
    samp[0] = indx
    samp[1] = indx2
    samp[2] = indx3


    Say I have a dictionary (samp) that has two groups: Group 0 and Group 1.
    The dictionary has indicies for values in the vector val.



    I want to pull out all of the values in val based on the given group in the dictionary by creating a 8 X 2 matrix,
    Where i have the group and the value in two columns in order by index so it looks like this:



    val  group
    10 2
    23 0
    45 2
    31 0
    87 0
    43 1
    1 1
    67 0
    82 1


    How do I go about doing this?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      The following is a simplified example:



      val = [10,23,45,31,78,43,1,67,82]

      indx = [1,4,5,8]
      indx2 = [3,6,7]
      indx3 = [0,2]

      samp = {}
      samp[0] = indx
      samp[1] = indx2
      samp[2] = indx3


      Say I have a dictionary (samp) that has two groups: Group 0 and Group 1.
      The dictionary has indicies for values in the vector val.



      I want to pull out all of the values in val based on the given group in the dictionary by creating a 8 X 2 matrix,
      Where i have the group and the value in two columns in order by index so it looks like this:



      val  group
      10 2
      23 0
      45 2
      31 0
      87 0
      43 1
      1 1
      67 0
      82 1


      How do I go about doing this?










      share|improve this question















      The following is a simplified example:



      val = [10,23,45,31,78,43,1,67,82]

      indx = [1,4,5,8]
      indx2 = [3,6,7]
      indx3 = [0,2]

      samp = {}
      samp[0] = indx
      samp[1] = indx2
      samp[2] = indx3


      Say I have a dictionary (samp) that has two groups: Group 0 and Group 1.
      The dictionary has indicies for values in the vector val.



      I want to pull out all of the values in val based on the given group in the dictionary by creating a 8 X 2 matrix,
      Where i have the group and the value in two columns in order by index so it looks like this:



      val  group
      10 2
      23 0
      45 2
      31 0
      87 0
      43 1
      1 1
      67 0
      82 1


      How do I go about doing this?







      python pandas dictionary numpy-ndarray






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 5:06

























      asked Nov 10 at 4:59









      Sheila

      84561327




      84561327
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          One way to get this



          [(j, next(k for k,v in samp.items() if i in v)) for i,j in enumerate(val)]


          Output:



          [(10, 2),
          (23, 0),
          (45, 2),
          (31, 1),
          (78, 0),
          (43, 0),
          (1, 1),
          (67, 1),
          (82, 0)]





          share|improve this answer




























            up vote
            0
            down vote













            Here is a solution without using pandas that outputs a (8,2) numpy matrix:



            val = [10,23,45,31,78,43,1,67,82]

            indx = [1,4,5,8]
            indx2 = [3,6,7]
            indx3 = [0,2]

            indices = [indx, indx2, indx3]

            def get_group(x):
            for i,indx_arr in enumerate(indices):
            if x in indx_arr:
            return i

            pairs = [(v,get_group(i)) for i,v in enumerate(val)]
            np.asarray(pairs)

            array([[10, 2],
            [23, 0],
            [45, 2],
            [31, 1],
            [78, 0],
            [43, 0],
            [ 1, 1],
            [67, 1],
            [82, 0]])





            share|improve this answer




























              up vote
              0
              down vote













              Use dictionary comprehension to reverse the key, value pairs in dictionary and then use map:



              df = pd.DataFrame(val,columns=['val'])
              d = {value1:key for key,value in samp.items() for value1 in value}
              df['group'] = df.index.map(d)

              print(df)
              val group
              0 10 2
              1 23 0
              2 45 2
              3 31 1
              4 78 0
              5 43 0
              6 1 1
              7 67 1




              print(d)
              {1: 0, 4: 0, 5: 0, 8: 0, 3: 1, 6: 1, 7: 1, 0: 2, 2: 2}


              What if the values are numpy arrays:



              indx = np.array([1,4,5,8])
              indx2 = np.array([3,6,7])
              indx3 = np.array([0,2])

              samp = {}
              samp[0] = indx
              samp[1] = indx2
              samp[2] = indx3

              print(samp)
              {0: array([1, 4, 5, 8]), 1: array([3, 6, 7]), 2: array([0, 2])}

              d = {value1:key for key,value in samp.items() for value1 in value}

              print(d)
              {1: 0, 4: 0, 5: 0, 8: 0, 3: 1, 6: 1, 7: 1, 0: 2, 2: 2}





              share|improve this answer























              • Thanks @Sandeep. What if the values in the dictonaries are numpy arrays? I noticed that .items() is not supported for np.arrays
                – Sheila
                Nov 10 at 5:18










              • @Sheila It would work even if the values are numpy arrays check the update.
                – Sandeep Kadapa
                Nov 10 at 5:20











              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%2f53236115%2fcreating-new-matrix-based-on-dictionary-values-and-a-list%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








              up vote
              1
              down vote



              accepted










              One way to get this



              [(j, next(k for k,v in samp.items() if i in v)) for i,j in enumerate(val)]


              Output:



              [(10, 2),
              (23, 0),
              (45, 2),
              (31, 1),
              (78, 0),
              (43, 0),
              (1, 1),
              (67, 1),
              (82, 0)]





              share|improve this answer

























                up vote
                1
                down vote



                accepted










                One way to get this



                [(j, next(k for k,v in samp.items() if i in v)) for i,j in enumerate(val)]


                Output:



                [(10, 2),
                (23, 0),
                (45, 2),
                (31, 1),
                (78, 0),
                (43, 0),
                (1, 1),
                (67, 1),
                (82, 0)]





                share|improve this answer























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  One way to get this



                  [(j, next(k for k,v in samp.items() if i in v)) for i,j in enumerate(val)]


                  Output:



                  [(10, 2),
                  (23, 0),
                  (45, 2),
                  (31, 1),
                  (78, 0),
                  (43, 0),
                  (1, 1),
                  (67, 1),
                  (82, 0)]





                  share|improve this answer












                  One way to get this



                  [(j, next(k for k,v in samp.items() if i in v)) for i,j in enumerate(val)]


                  Output:



                  [(10, 2),
                  (23, 0),
                  (45, 2),
                  (31, 1),
                  (78, 0),
                  (43, 0),
                  (1, 1),
                  (67, 1),
                  (82, 0)]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 5:26









                  Transhuman

                  2,6111411




                  2,6111411
























                      up vote
                      0
                      down vote













                      Here is a solution without using pandas that outputs a (8,2) numpy matrix:



                      val = [10,23,45,31,78,43,1,67,82]

                      indx = [1,4,5,8]
                      indx2 = [3,6,7]
                      indx3 = [0,2]

                      indices = [indx, indx2, indx3]

                      def get_group(x):
                      for i,indx_arr in enumerate(indices):
                      if x in indx_arr:
                      return i

                      pairs = [(v,get_group(i)) for i,v in enumerate(val)]
                      np.asarray(pairs)

                      array([[10, 2],
                      [23, 0],
                      [45, 2],
                      [31, 1],
                      [78, 0],
                      [43, 0],
                      [ 1, 1],
                      [67, 1],
                      [82, 0]])





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Here is a solution without using pandas that outputs a (8,2) numpy matrix:



                        val = [10,23,45,31,78,43,1,67,82]

                        indx = [1,4,5,8]
                        indx2 = [3,6,7]
                        indx3 = [0,2]

                        indices = [indx, indx2, indx3]

                        def get_group(x):
                        for i,indx_arr in enumerate(indices):
                        if x in indx_arr:
                        return i

                        pairs = [(v,get_group(i)) for i,v in enumerate(val)]
                        np.asarray(pairs)

                        array([[10, 2],
                        [23, 0],
                        [45, 2],
                        [31, 1],
                        [78, 0],
                        [43, 0],
                        [ 1, 1],
                        [67, 1],
                        [82, 0]])





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Here is a solution without using pandas that outputs a (8,2) numpy matrix:



                          val = [10,23,45,31,78,43,1,67,82]

                          indx = [1,4,5,8]
                          indx2 = [3,6,7]
                          indx3 = [0,2]

                          indices = [indx, indx2, indx3]

                          def get_group(x):
                          for i,indx_arr in enumerate(indices):
                          if x in indx_arr:
                          return i

                          pairs = [(v,get_group(i)) for i,v in enumerate(val)]
                          np.asarray(pairs)

                          array([[10, 2],
                          [23, 0],
                          [45, 2],
                          [31, 1],
                          [78, 0],
                          [43, 0],
                          [ 1, 1],
                          [67, 1],
                          [82, 0]])





                          share|improve this answer












                          Here is a solution without using pandas that outputs a (8,2) numpy matrix:



                          val = [10,23,45,31,78,43,1,67,82]

                          indx = [1,4,5,8]
                          indx2 = [3,6,7]
                          indx3 = [0,2]

                          indices = [indx, indx2, indx3]

                          def get_group(x):
                          for i,indx_arr in enumerate(indices):
                          if x in indx_arr:
                          return i

                          pairs = [(v,get_group(i)) for i,v in enumerate(val)]
                          np.asarray(pairs)

                          array([[10, 2],
                          [23, 0],
                          [45, 2],
                          [31, 1],
                          [78, 0],
                          [43, 0],
                          [ 1, 1],
                          [67, 1],
                          [82, 0]])






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 10 at 5:19









                          Dani G

                          427411




                          427411






















                              up vote
                              0
                              down vote













                              Use dictionary comprehension to reverse the key, value pairs in dictionary and then use map:



                              df = pd.DataFrame(val,columns=['val'])
                              d = {value1:key for key,value in samp.items() for value1 in value}
                              df['group'] = df.index.map(d)

                              print(df)
                              val group
                              0 10 2
                              1 23 0
                              2 45 2
                              3 31 1
                              4 78 0
                              5 43 0
                              6 1 1
                              7 67 1




                              print(d)
                              {1: 0, 4: 0, 5: 0, 8: 0, 3: 1, 6: 1, 7: 1, 0: 2, 2: 2}


                              What if the values are numpy arrays:



                              indx = np.array([1,4,5,8])
                              indx2 = np.array([3,6,7])
                              indx3 = np.array([0,2])

                              samp = {}
                              samp[0] = indx
                              samp[1] = indx2
                              samp[2] = indx3

                              print(samp)
                              {0: array([1, 4, 5, 8]), 1: array([3, 6, 7]), 2: array([0, 2])}

                              d = {value1:key for key,value in samp.items() for value1 in value}

                              print(d)
                              {1: 0, 4: 0, 5: 0, 8: 0, 3: 1, 6: 1, 7: 1, 0: 2, 2: 2}





                              share|improve this answer























                              • Thanks @Sandeep. What if the values in the dictonaries are numpy arrays? I noticed that .items() is not supported for np.arrays
                                – Sheila
                                Nov 10 at 5:18










                              • @Sheila It would work even if the values are numpy arrays check the update.
                                – Sandeep Kadapa
                                Nov 10 at 5:20















                              up vote
                              0
                              down vote













                              Use dictionary comprehension to reverse the key, value pairs in dictionary and then use map:



                              df = pd.DataFrame(val,columns=['val'])
                              d = {value1:key for key,value in samp.items() for value1 in value}
                              df['group'] = df.index.map(d)

                              print(df)
                              val group
                              0 10 2
                              1 23 0
                              2 45 2
                              3 31 1
                              4 78 0
                              5 43 0
                              6 1 1
                              7 67 1




                              print(d)
                              {1: 0, 4: 0, 5: 0, 8: 0, 3: 1, 6: 1, 7: 1, 0: 2, 2: 2}


                              What if the values are numpy arrays:



                              indx = np.array([1,4,5,8])
                              indx2 = np.array([3,6,7])
                              indx3 = np.array([0,2])

                              samp = {}
                              samp[0] = indx
                              samp[1] = indx2
                              samp[2] = indx3

                              print(samp)
                              {0: array([1, 4, 5, 8]), 1: array([3, 6, 7]), 2: array([0, 2])}

                              d = {value1:key for key,value in samp.items() for value1 in value}

                              print(d)
                              {1: 0, 4: 0, 5: 0, 8: 0, 3: 1, 6: 1, 7: 1, 0: 2, 2: 2}





                              share|improve this answer























                              • Thanks @Sandeep. What if the values in the dictonaries are numpy arrays? I noticed that .items() is not supported for np.arrays
                                – Sheila
                                Nov 10 at 5:18










                              • @Sheila It would work even if the values are numpy arrays check the update.
                                – Sandeep Kadapa
                                Nov 10 at 5:20













                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              Use dictionary comprehension to reverse the key, value pairs in dictionary and then use map:



                              df = pd.DataFrame(val,columns=['val'])
                              d = {value1:key for key,value in samp.items() for value1 in value}
                              df['group'] = df.index.map(d)

                              print(df)
                              val group
                              0 10 2
                              1 23 0
                              2 45 2
                              3 31 1
                              4 78 0
                              5 43 0
                              6 1 1
                              7 67 1




                              print(d)
                              {1: 0, 4: 0, 5: 0, 8: 0, 3: 1, 6: 1, 7: 1, 0: 2, 2: 2}


                              What if the values are numpy arrays:



                              indx = np.array([1,4,5,8])
                              indx2 = np.array([3,6,7])
                              indx3 = np.array([0,2])

                              samp = {}
                              samp[0] = indx
                              samp[1] = indx2
                              samp[2] = indx3

                              print(samp)
                              {0: array([1, 4, 5, 8]), 1: array([3, 6, 7]), 2: array([0, 2])}

                              d = {value1:key for key,value in samp.items() for value1 in value}

                              print(d)
                              {1: 0, 4: 0, 5: 0, 8: 0, 3: 1, 6: 1, 7: 1, 0: 2, 2: 2}





                              share|improve this answer














                              Use dictionary comprehension to reverse the key, value pairs in dictionary and then use map:



                              df = pd.DataFrame(val,columns=['val'])
                              d = {value1:key for key,value in samp.items() for value1 in value}
                              df['group'] = df.index.map(d)

                              print(df)
                              val group
                              0 10 2
                              1 23 0
                              2 45 2
                              3 31 1
                              4 78 0
                              5 43 0
                              6 1 1
                              7 67 1




                              print(d)
                              {1: 0, 4: 0, 5: 0, 8: 0, 3: 1, 6: 1, 7: 1, 0: 2, 2: 2}


                              What if the values are numpy arrays:



                              indx = np.array([1,4,5,8])
                              indx2 = np.array([3,6,7])
                              indx3 = np.array([0,2])

                              samp = {}
                              samp[0] = indx
                              samp[1] = indx2
                              samp[2] = indx3

                              print(samp)
                              {0: array([1, 4, 5, 8]), 1: array([3, 6, 7]), 2: array([0, 2])}

                              d = {value1:key for key,value in samp.items() for value1 in value}

                              print(d)
                              {1: 0, 4: 0, 5: 0, 8: 0, 3: 1, 6: 1, 7: 1, 0: 2, 2: 2}






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 10 at 5:21

























                              answered Nov 10 at 5:09









                              Sandeep Kadapa

                              5,642427




                              5,642427












                              • Thanks @Sandeep. What if the values in the dictonaries are numpy arrays? I noticed that .items() is not supported for np.arrays
                                – Sheila
                                Nov 10 at 5:18










                              • @Sheila It would work even if the values are numpy arrays check the update.
                                – Sandeep Kadapa
                                Nov 10 at 5:20


















                              • Thanks @Sandeep. What if the values in the dictonaries are numpy arrays? I noticed that .items() is not supported for np.arrays
                                – Sheila
                                Nov 10 at 5:18










                              • @Sheila It would work even if the values are numpy arrays check the update.
                                – Sandeep Kadapa
                                Nov 10 at 5:20
















                              Thanks @Sandeep. What if the values in the dictonaries are numpy arrays? I noticed that .items() is not supported for np.arrays
                              – Sheila
                              Nov 10 at 5:18




                              Thanks @Sandeep. What if the values in the dictonaries are numpy arrays? I noticed that .items() is not supported for np.arrays
                              – Sheila
                              Nov 10 at 5:18












                              @Sheila It would work even if the values are numpy arrays check the update.
                              – Sandeep Kadapa
                              Nov 10 at 5:20




                              @Sheila It would work even if the values are numpy arrays check the update.
                              – Sandeep Kadapa
                              Nov 10 at 5:20


















                              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%2f53236115%2fcreating-new-matrix-based-on-dictionary-values-and-a-list%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







                              這個網誌中的熱門文章

                              Xamarin.form Move up view when keyboard appear

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Anylogic : not able to use stopDelay()