StackOverflow

### Answer rating: 188

### Answer rating: 145

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

well u can do this :

```
newrow = [1,2,3]
A = numpy.vstack([A, newrow])
```

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]])
```

Learn how data literacy is changing the world and giving you a better understanding of life's biggest problems in this "Important and Comprehensive" Guide to Statistical Thinking (New York). The bi...

28/08/2021

Python Workout isn’t designed to teach you Python, although I hope and expect that you’ll learn quite a bit along the way. It is meant to help you improve your understand- ing of Python and how to...

23/09/2020

I have developed this book to investigate Mesos-based cluster development and integration. I found that data center operating system (DCOS; and it’s command-line interface [CLI]) was a natural progr...

10/07/2020

Python Crash Course is the world's best-selling guide to the Python programming language. This quick and in-depth introduction to Python programming will get you started writing programs, solving prob...

11/08/2021

X
# Submit new EBook