Creating NumPy arrays
There is more than one way to create NumPy arrays. The following are some of the ways:
- Transforming an existing data structure into an array
- Creating an array from scratch and populating it with default or calculated values
- Uploading some data from a disk into an array
If you are going to transform an existing data structure, the odds are in favor of you working with a structured list or a pandas DataFrame.
From lists to unidimensional arrays
One of the most common situations you will encounter when working with data is the transforming of a list into an array.
When operating such a transformation, it is important to consider the objects the lists contain because this will determine the dimensionality and the dtype of the resulting array.
Let's start with the first example of a list containing just integers:
In: import numpy as np In: # Transform a list into a uni-dimensional array list_of_ints = [1,2,3] Array_1 = np.array(list_of_ints) In: Array_1 Out: array([1, 2, 3...