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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.