Stacking NumPy arrays

When operating with two-dimensional data arrays, there are some common operations, such as the adding of data and variables, that NumPy functions can render easily and quickly.

The most common such operation is the addition of more cases to your array:

  1. Let's start off by creating an array:
In: import numpy as np    dataset = np.arange(10*5).reshape(10,5)  
  1. Now, let's add a single row and a bunch of rows that are to be concatenated after each other:
In: single_line = np.arange(1*5).reshape(1,5)    a_few_lines = np.arange(3*5).reshape(3,5)  
  1. We can first try to add a single line:
In: np.vstack((dataset,single_line))  
  1. All you have to do is provide a tuple containing the vertical array preceding it and the one following it. ...

Get Python Data Science Essentials - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.