How to add values to a new column after n rows?











up vote
0
down vote

favorite
1












I have this loop that continuously checks api data and add it to an array.
Then I calculate an indicator based on one column of the array that I want to add the the array. The issue only is that the indicator need 10 data point to get going.



import numpy as np

list_b =
i = 0

while True:
i += 1
list_a =

list_a.append(i)
list_a.append(33)
list_a.append(44)
list_b.append(list_a)
array = np.array(list_b, dtype=float)

if len(array) > 10:
x = indicator_function(array[:,0])
np.append(x) # in a 4th column

if i == 15:
break

print(array.shape)
print(len(array))
print(array)


The issue is that I cant append to the array because it doesnt have the same dimensions as the array before. So how can I extend the array by one column or insert the new indicators values in a new column after 10 rows.



The array should be formed like this:



1 33 44
2 33 44
...
9 33 44
10 33 44
11 33 44 x
12 33 44 x









share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    I have this loop that continuously checks api data and add it to an array.
    Then I calculate an indicator based on one column of the array that I want to add the the array. The issue only is that the indicator need 10 data point to get going.



    import numpy as np

    list_b =
    i = 0

    while True:
    i += 1
    list_a =

    list_a.append(i)
    list_a.append(33)
    list_a.append(44)
    list_b.append(list_a)
    array = np.array(list_b, dtype=float)

    if len(array) > 10:
    x = indicator_function(array[:,0])
    np.append(x) # in a 4th column

    if i == 15:
    break

    print(array.shape)
    print(len(array))
    print(array)


    The issue is that I cant append to the array because it doesnt have the same dimensions as the array before. So how can I extend the array by one column or insert the new indicators values in a new column after 10 rows.



    The array should be formed like this:



    1 33 44
    2 33 44
    ...
    9 33 44
    10 33 44
    11 33 44 x
    12 33 44 x









    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have this loop that continuously checks api data and add it to an array.
      Then I calculate an indicator based on one column of the array that I want to add the the array. The issue only is that the indicator need 10 data point to get going.



      import numpy as np

      list_b =
      i = 0

      while True:
      i += 1
      list_a =

      list_a.append(i)
      list_a.append(33)
      list_a.append(44)
      list_b.append(list_a)
      array = np.array(list_b, dtype=float)

      if len(array) > 10:
      x = indicator_function(array[:,0])
      np.append(x) # in a 4th column

      if i == 15:
      break

      print(array.shape)
      print(len(array))
      print(array)


      The issue is that I cant append to the array because it doesnt have the same dimensions as the array before. So how can I extend the array by one column or insert the new indicators values in a new column after 10 rows.



      The array should be formed like this:



      1 33 44
      2 33 44
      ...
      9 33 44
      10 33 44
      11 33 44 x
      12 33 44 x









      share|improve this question













      I have this loop that continuously checks api data and add it to an array.
      Then I calculate an indicator based on one column of the array that I want to add the the array. The issue only is that the indicator need 10 data point to get going.



      import numpy as np

      list_b =
      i = 0

      while True:
      i += 1
      list_a =

      list_a.append(i)
      list_a.append(33)
      list_a.append(44)
      list_b.append(list_a)
      array = np.array(list_b, dtype=float)

      if len(array) > 10:
      x = indicator_function(array[:,0])
      np.append(x) # in a 4th column

      if i == 15:
      break

      print(array.shape)
      print(len(array))
      print(array)


      The issue is that I cant append to the array because it doesnt have the same dimensions as the array before. So how can I extend the array by one column or insert the new indicators values in a new column after 10 rows.



      The array should be formed like this:



      1 33 44
      2 33 44
      ...
      9 33 44
      10 33 44
      11 33 44 x
      12 33 44 x






      numpy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 2:54









      Sungod3k

      34




      34
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote













          From what I understand, numpy doesn't allow for jagged arrays (arrays with a changing number of columns). You might have to create an entirely new array. Or, if you want to do something crazy, you could move the if statement up above list_b.append(list_a) and change it to



          if (i > 10):
          x = indicator_function(array[:,0])
          list_a.append(x)
          else:
          list_a.append(None)





          share|improve this answer




























            up vote
            0
            down vote













            if i < 10:   
            list_a.append(i)
            list_a.append(33)
            list_a.append(44)
            list_a.append(55)
            list_b.append(list_a)
            array = np.array(list_b, dtype=float)

            if i > 10:
            list_a.append(i)
            list_a.append(33)
            list_a.append(44)
            list_a.append(66)
            list_b.append(list_a)
            array = np.array(list_b, dtype=float)


            @sahil yes that works.






            share|improve this answer




























              up vote
              0
              down vote













              There's no builtin support for proper jagged arrays in numpy. The best bet for your application is probably a masked array:



              import itertools
              import numpy.ma as ma

              array = None
              list_b =

              def indicator_function(arr):
              return -1

              for i in itertools.count():
              list_a =
              list_b.append(list_a)

              list_a.append(i)
              list_a.append(33)
              list_a.append(44)
              list_a.append(np.nan)
              array = ma.MaskedArray(list_b)

              # assign mask or indicator_function, as appropriate
              ix = 10 + 1
              array[:ix, -1] = ma.masked
              array[ix:, -1] = indicator_function(array[:,0])

              if i == 15:
              break


              This results in a masked array that looks like:



              [[0.0, 33.0, 44.0, --],
              [1.0, 33.0, 44.0, --],
              [2.0, 33.0, 44.0, --],
              [3.0, 33.0, 44.0, --],
              [4.0, 33.0, 44.0, --],
              [5.0, 33.0, 44.0, --],
              [6.0, 33.0, 44.0, --],
              [7.0, 33.0, 44.0, --],
              [8.0, 33.0, 44.0, --],
              [9.0, 33.0, 44.0, --],
              [10.0, 33.0, 44.0, --],
              [11.0, 33.0, 44.0, -1.0],
              [12.0, 33.0, 44.0, -1.0],
              [13.0, 33.0, 44.0, -1.0],
              [14.0, 33.0, 44.0, -1.0],
              [15.0, 33.0, 44.0, -1.0]]





              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',
                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%2f53245450%2fhow-to-add-values-to-a-new-column-after-n-rows%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
                0
                down vote













                From what I understand, numpy doesn't allow for jagged arrays (arrays with a changing number of columns). You might have to create an entirely new array. Or, if you want to do something crazy, you could move the if statement up above list_b.append(list_a) and change it to



                if (i > 10):
                x = indicator_function(array[:,0])
                list_a.append(x)
                else:
                list_a.append(None)





                share|improve this answer

























                  up vote
                  0
                  down vote













                  From what I understand, numpy doesn't allow for jagged arrays (arrays with a changing number of columns). You might have to create an entirely new array. Or, if you want to do something crazy, you could move the if statement up above list_b.append(list_a) and change it to



                  if (i > 10):
                  x = indicator_function(array[:,0])
                  list_a.append(x)
                  else:
                  list_a.append(None)





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    From what I understand, numpy doesn't allow for jagged arrays (arrays with a changing number of columns). You might have to create an entirely new array. Or, if you want to do something crazy, you could move the if statement up above list_b.append(list_a) and change it to



                    if (i > 10):
                    x = indicator_function(array[:,0])
                    list_a.append(x)
                    else:
                    list_a.append(None)





                    share|improve this answer












                    From what I understand, numpy doesn't allow for jagged arrays (arrays with a changing number of columns). You might have to create an entirely new array. Or, if you want to do something crazy, you could move the if statement up above list_b.append(list_a) and change it to



                    if (i > 10):
                    x = indicator_function(array[:,0])
                    list_a.append(x)
                    else:
                    list_a.append(None)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 11 at 3:24









                    Sahil Makhijani

                    538




                    538
























                        up vote
                        0
                        down vote













                        if i < 10:   
                        list_a.append(i)
                        list_a.append(33)
                        list_a.append(44)
                        list_a.append(55)
                        list_b.append(list_a)
                        array = np.array(list_b, dtype=float)

                        if i > 10:
                        list_a.append(i)
                        list_a.append(33)
                        list_a.append(44)
                        list_a.append(66)
                        list_b.append(list_a)
                        array = np.array(list_b, dtype=float)


                        @sahil yes that works.






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          if i < 10:   
                          list_a.append(i)
                          list_a.append(33)
                          list_a.append(44)
                          list_a.append(55)
                          list_b.append(list_a)
                          array = np.array(list_b, dtype=float)

                          if i > 10:
                          list_a.append(i)
                          list_a.append(33)
                          list_a.append(44)
                          list_a.append(66)
                          list_b.append(list_a)
                          array = np.array(list_b, dtype=float)


                          @sahil yes that works.






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            if i < 10:   
                            list_a.append(i)
                            list_a.append(33)
                            list_a.append(44)
                            list_a.append(55)
                            list_b.append(list_a)
                            array = np.array(list_b, dtype=float)

                            if i > 10:
                            list_a.append(i)
                            list_a.append(33)
                            list_a.append(44)
                            list_a.append(66)
                            list_b.append(list_a)
                            array = np.array(list_b, dtype=float)


                            @sahil yes that works.






                            share|improve this answer












                            if i < 10:   
                            list_a.append(i)
                            list_a.append(33)
                            list_a.append(44)
                            list_a.append(55)
                            list_b.append(list_a)
                            array = np.array(list_b, dtype=float)

                            if i > 10:
                            list_a.append(i)
                            list_a.append(33)
                            list_a.append(44)
                            list_a.append(66)
                            list_b.append(list_a)
                            array = np.array(list_b, dtype=float)


                            @sahil yes that works.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 11 at 3:41









                            Sungod3k

                            34




                            34






















                                up vote
                                0
                                down vote













                                There's no builtin support for proper jagged arrays in numpy. The best bet for your application is probably a masked array:



                                import itertools
                                import numpy.ma as ma

                                array = None
                                list_b =

                                def indicator_function(arr):
                                return -1

                                for i in itertools.count():
                                list_a =
                                list_b.append(list_a)

                                list_a.append(i)
                                list_a.append(33)
                                list_a.append(44)
                                list_a.append(np.nan)
                                array = ma.MaskedArray(list_b)

                                # assign mask or indicator_function, as appropriate
                                ix = 10 + 1
                                array[:ix, -1] = ma.masked
                                array[ix:, -1] = indicator_function(array[:,0])

                                if i == 15:
                                break


                                This results in a masked array that looks like:



                                [[0.0, 33.0, 44.0, --],
                                [1.0, 33.0, 44.0, --],
                                [2.0, 33.0, 44.0, --],
                                [3.0, 33.0, 44.0, --],
                                [4.0, 33.0, 44.0, --],
                                [5.0, 33.0, 44.0, --],
                                [6.0, 33.0, 44.0, --],
                                [7.0, 33.0, 44.0, --],
                                [8.0, 33.0, 44.0, --],
                                [9.0, 33.0, 44.0, --],
                                [10.0, 33.0, 44.0, --],
                                [11.0, 33.0, 44.0, -1.0],
                                [12.0, 33.0, 44.0, -1.0],
                                [13.0, 33.0, 44.0, -1.0],
                                [14.0, 33.0, 44.0, -1.0],
                                [15.0, 33.0, 44.0, -1.0]]





                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  There's no builtin support for proper jagged arrays in numpy. The best bet for your application is probably a masked array:



                                  import itertools
                                  import numpy.ma as ma

                                  array = None
                                  list_b =

                                  def indicator_function(arr):
                                  return -1

                                  for i in itertools.count():
                                  list_a =
                                  list_b.append(list_a)

                                  list_a.append(i)
                                  list_a.append(33)
                                  list_a.append(44)
                                  list_a.append(np.nan)
                                  array = ma.MaskedArray(list_b)

                                  # assign mask or indicator_function, as appropriate
                                  ix = 10 + 1
                                  array[:ix, -1] = ma.masked
                                  array[ix:, -1] = indicator_function(array[:,0])

                                  if i == 15:
                                  break


                                  This results in a masked array that looks like:



                                  [[0.0, 33.0, 44.0, --],
                                  [1.0, 33.0, 44.0, --],
                                  [2.0, 33.0, 44.0, --],
                                  [3.0, 33.0, 44.0, --],
                                  [4.0, 33.0, 44.0, --],
                                  [5.0, 33.0, 44.0, --],
                                  [6.0, 33.0, 44.0, --],
                                  [7.0, 33.0, 44.0, --],
                                  [8.0, 33.0, 44.0, --],
                                  [9.0, 33.0, 44.0, --],
                                  [10.0, 33.0, 44.0, --],
                                  [11.0, 33.0, 44.0, -1.0],
                                  [12.0, 33.0, 44.0, -1.0],
                                  [13.0, 33.0, 44.0, -1.0],
                                  [14.0, 33.0, 44.0, -1.0],
                                  [15.0, 33.0, 44.0, -1.0]]





                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    There's no builtin support for proper jagged arrays in numpy. The best bet for your application is probably a masked array:



                                    import itertools
                                    import numpy.ma as ma

                                    array = None
                                    list_b =

                                    def indicator_function(arr):
                                    return -1

                                    for i in itertools.count():
                                    list_a =
                                    list_b.append(list_a)

                                    list_a.append(i)
                                    list_a.append(33)
                                    list_a.append(44)
                                    list_a.append(np.nan)
                                    array = ma.MaskedArray(list_b)

                                    # assign mask or indicator_function, as appropriate
                                    ix = 10 + 1
                                    array[:ix, -1] = ma.masked
                                    array[ix:, -1] = indicator_function(array[:,0])

                                    if i == 15:
                                    break


                                    This results in a masked array that looks like:



                                    [[0.0, 33.0, 44.0, --],
                                    [1.0, 33.0, 44.0, --],
                                    [2.0, 33.0, 44.0, --],
                                    [3.0, 33.0, 44.0, --],
                                    [4.0, 33.0, 44.0, --],
                                    [5.0, 33.0, 44.0, --],
                                    [6.0, 33.0, 44.0, --],
                                    [7.0, 33.0, 44.0, --],
                                    [8.0, 33.0, 44.0, --],
                                    [9.0, 33.0, 44.0, --],
                                    [10.0, 33.0, 44.0, --],
                                    [11.0, 33.0, 44.0, -1.0],
                                    [12.0, 33.0, 44.0, -1.0],
                                    [13.0, 33.0, 44.0, -1.0],
                                    [14.0, 33.0, 44.0, -1.0],
                                    [15.0, 33.0, 44.0, -1.0]]





                                    share|improve this answer












                                    There's no builtin support for proper jagged arrays in numpy. The best bet for your application is probably a masked array:



                                    import itertools
                                    import numpy.ma as ma

                                    array = None
                                    list_b =

                                    def indicator_function(arr):
                                    return -1

                                    for i in itertools.count():
                                    list_a =
                                    list_b.append(list_a)

                                    list_a.append(i)
                                    list_a.append(33)
                                    list_a.append(44)
                                    list_a.append(np.nan)
                                    array = ma.MaskedArray(list_b)

                                    # assign mask or indicator_function, as appropriate
                                    ix = 10 + 1
                                    array[:ix, -1] = ma.masked
                                    array[ix:, -1] = indicator_function(array[:,0])

                                    if i == 15:
                                    break


                                    This results in a masked array that looks like:



                                    [[0.0, 33.0, 44.0, --],
                                    [1.0, 33.0, 44.0, --],
                                    [2.0, 33.0, 44.0, --],
                                    [3.0, 33.0, 44.0, --],
                                    [4.0, 33.0, 44.0, --],
                                    [5.0, 33.0, 44.0, --],
                                    [6.0, 33.0, 44.0, --],
                                    [7.0, 33.0, 44.0, --],
                                    [8.0, 33.0, 44.0, --],
                                    [9.0, 33.0, 44.0, --],
                                    [10.0, 33.0, 44.0, --],
                                    [11.0, 33.0, 44.0, -1.0],
                                    [12.0, 33.0, 44.0, -1.0],
                                    [13.0, 33.0, 44.0, -1.0],
                                    [14.0, 33.0, 44.0, -1.0],
                                    [15.0, 33.0, 44.0, -1.0]]






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 11 at 3:48









                                    tel

                                    2,9131426




                                    2,9131426






























                                         

                                        draft saved


                                        draft discarded



















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245450%2fhow-to-add-values-to-a-new-column-after-n-rows%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

                                        Xamarin.iOS Cant Deploy on Iphone

                                        Glorious Revolution

                                        Dulmage-Mendelsohn matrix decomposition in Python