Numpy - add row to array
Asked Answered
A

10

248

How does one add rows to a numpy array?

I have an array A:

A = array([[0, 1, 2], [0, 2, 0]])

I wish to add rows to this array from another array X if the first element of each row in X meets a specific condition.

Numpy arrays do not have a method 'append' like that of lists, or so it seems.

If A and X were lists I would merely do:

for i in X:
    if i[0] < 3:
        A.append(i)

Is there a numpythonic way to do the equivalent?

Thanks, S ;-)

Aberdeen answered 7/10, 2010 at 12:9 Comment(1)
See also #8486794Daimon
A
154

What is X? If it is a 2D-array, how can you then compare its row to a number: i < 3?

EDIT after OP's comment:

A = array([[0, 1, 2], [0, 2, 0]])
X = array([[0, 1, 2], [1, 2, 0], [2, 1, 2], [3, 2, 0]])

add to A all rows from X where the first element < 3:

import numpy as np
A = np.vstack((A, X[X[:,0] < 3]))

# returns: 
array([[0, 1, 2],
       [0, 2, 0],
       [0, 1, 2],
       [1, 2, 0],
       [2, 1, 2]])
Anchorite answered 7/10, 2010 at 12:14 Comment(2)
Sorry good point! Assume a 2D array of which the first element of each row must meet a condition. I will edit that. Thanks, S ;-)Aberdeen
@DarrenJ.Fitzpatrick Keep in mind that by doing this type of manipulation you work against the good work Numpy does in pre-allocating memory for your existing array A. Clearly for small problem like in this answer this isn't a problem, but it can be more troubling for large data.Peisch
E
256

You can do this:

newrow = [1, 2, 3]
A = numpy.vstack([A, newrow])
Ensepulcher answered 7/10, 2010 at 12:15 Comment(2)
@Kris Why is it deprecated? I see nothing in docsLanner
@Lanner To be honest, I don't know. I was here looking for answers same as you :-). I can't remember now why I wrote above comment. I must have seen in the docs its deprecated. But looking at the docs now... it doesn't say so. Is it possible they deprecated it, then changed their minds again and decided it would be too annoying to too many folks to deprecate and remove it?Gabion
A
154

What is X? If it is a 2D-array, how can you then compare its row to a number: i < 3?

EDIT after OP's comment:

A = array([[0, 1, 2], [0, 2, 0]])
X = array([[0, 1, 2], [1, 2, 0], [2, 1, 2], [3, 2, 0]])

add to A all rows from X where the first element < 3:

import numpy as np
A = np.vstack((A, X[X[:,0] < 3]))

# returns: 
array([[0, 1, 2],
       [0, 2, 0],
       [0, 1, 2],
       [1, 2, 0],
       [2, 1, 2]])
Anchorite answered 7/10, 2010 at 12:14 Comment(2)
Sorry good point! Assume a 2D array of which the first element of each row must meet a condition. I will edit that. Thanks, S ;-)Aberdeen
@DarrenJ.Fitzpatrick Keep in mind that by doing this type of manipulation you work against the good work Numpy does in pre-allocating memory for your existing array A. Clearly for small problem like in this answer this isn't a problem, but it can be more troubling for large data.Peisch
O
59

As this question is been 7 years before, in the latest version which I am using is numpy version 1.13, and python3, I am doing the same thing with adding a row to a matrix, remember to put a double bracket to the second argument, otherwise, it will raise dimension error.

In here I am adding on matrix A

1 2 3
4 5 6

with a row

7 8 9

same usage in np.r_

A = [[1, 2, 3], [4, 5, 6]]
np.append(A, [[7, 8, 9]], axis=0)

    >> array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])
#or 
np.r_[A,[[7,8,9]]]

Just to someone's intersted, if you would like to add a column,

array = np.c_[A,np.zeros(#A's row size)]

following what we did before on matrix A, adding a column to it

np.c_[A, [2,8]]

>> array([[1, 2, 3, 2],
          [4, 5, 6, 8]])

If you want to prepend, you can just flip the order of the arguments, i.e.:

np.r_([[7, 8, 9]], A)

    >> array([[7, 8, 9],
             [1, 2, 3],
             [4, 5, 6]])
Onomatology answered 16/12, 2017 at 11:16 Comment(0)
P
23

If no calculations are necessary after every row, it's much quicker to add rows in python, then convert to numpy. Here are timing tests using python 3.6 vs. numpy 1.14, adding 100 rows, one at a time:

import numpy as np 
from time import perf_counter, sleep

def time_it():
    # Compare performance of two methods for adding rows to numpy array
    py_array = [[0, 1, 2], [0, 2, 0]]
    py_row = [4, 5, 6]
    numpy_array = np.array(py_array)
    numpy_row = np.array([4,5,6])
    n_loops = 100

    start_clock = perf_counter()
    for count in range(0, n_loops):
       numpy_array = np.vstack([numpy_array, numpy_row]) # 5.8 micros
    duration = perf_counter() - start_clock
    print('numpy 1.14 takes {:.3f} micros per row'.format(duration * 1e6 / n_loops))

    start_clock = perf_counter()
    for count in range(0, n_loops):
        py_array.append(py_row) # .15 micros
    numpy_array = np.array(py_array) # 43.9 micros       
    duration = perf_counter() - start_clock
    print('python 3.6 takes {:.3f} micros per row'.format(duration * 1e6 / n_loops))
    sleep(15)

#time_it() prints:

numpy 1.14 takes 5.971 micros per row
python 3.6 takes 0.694 micros per row

So, the simple solution to the original question, from seven years ago, is to use vstack() to add a new row after converting the row to a numpy array. But a more realistic solution should consider vstack's poor performance under those circumstances. If you don't need to run data analysis on the array after every addition, it is better to buffer the new rows to a python list of rows (a list of lists, really), and add them as a group to the numpy array using vstack() before doing any data analysis.

Photic answered 3/3, 2018 at 16:40 Comment(0)
C
11

You can also do this:

newrow = [1,2,3]
A = numpy.concatenate((A,newrow))
Curculio answered 14/12, 2011 at 17:0 Comment(4)
hmmm. when I tried this, it just added to the end of A, rather than adding a new row as OP requested.Kilovoltampere
probably np.concatenate((A,newrow), axis=0)Dardan
As of numpy version 1.12.1 (and in Python 3), it seems like attempting to concatenate a vector to a matrix raises ValueError: all the input arrays must have same number of dimensions. It looks like it wants the vector to be reshaped explicitly into a column or row vector before it's willing to concatenate it.Kelso
@Kelso you can fix that by using double square brackets as per the answer from @Flora PJ Li https://mcmap.net/q/116016/-numpy-add-row-to-array. newrow = [[1,2,3]]Mascarenas
N
8
import numpy as np
array_ = np.array([[1,2,3]])
add_row = np.array([[4,5,6]])

array_ = np.concatenate((array_, add_row), axis=0)
Nullification answered 27/1, 2019 at 15:0 Comment(0)
M
5

I use 'np.vstack' which is faster, EX:

import numpy as np

input_array=np.array([1,2,3])
new_row= np.array([4,5,6])

new_array=np.vstack([input_array, new_row])
M16 answered 12/2, 2018 at 1:26 Comment(0)
R
4

I use numpy.insert(arr, i, the_object_to_be_added, axis) in order to insert object_to_be_added at the i'th row(axis=0) or column(axis=1)

import numpy as np

a = np.array([[1, 2, 3], [5, 4, 6]])
# array([[1, 2, 3],
#        [5, 4, 6]])

np.insert(a, 1, [55, 66], axis=1)
# array([[ 1, 55,  2,  3],
#        [ 5, 66,  4,  6]])

np.insert(a, 2, [50, 60, 70], axis=0)
# array([[ 1,  2,  3],
#        [ 5,  4,  6],
#        [50, 60, 70]])

Too old discussion, but I hope it helps someone.

Rael answered 22/10, 2020 at 20:36 Comment(0)
C
3

If you can do the construction in a single operation, then something like the vstack-with-fancy-indexing answer is a fine approach. But if your condition is more complicated or your rows come in on the fly, you may want to grow the array. In fact the numpythonic way to do something like this - dynamically grow an array - is to dynamically grow a list:

A = np.array([[1,2,3],[4,5,6]])
Alist = [r for r in A]
for i in range(100):
    newrow = np.arange(3)+i
    if i%5:
        Alist.append(newrow)
A = np.array(Alist)
del Alist

Lists are highly optimized for this kind of access pattern; you don't have convenient numpy multidimensional indexing while in list form, but for as long as you're appending it's hard to do better than a list of row arrays.

Cantilena answered 25/6, 2014 at 15:48 Comment(0)
W
2

You can use numpy.append() to append a row to numpty array and reshape to a matrix later on.

import numpy as np
a = np.array([1,2])
a = np.append(a, [3,4])
print a
# [1,2,3,4]
# in your example
A = [1,2]
for row in X:
    A = np.append(A, row)
Wallache answered 11/7, 2016 at 18:9 Comment(1)
As the array shape is changed during the addition, it's not really a solution to adding a row.Lozenge

© 2022 - 2024 — McMap. All rights reserved.