O'Reilly logo

Learning pandas by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating NumPy arrays and performing basic array operations

A NumPy array can be created using multiple techniques. The following code creates a new NumPy array object from a Python list:

In [4]:
   # a simple array
   a1 = np.array([1, 2, 3, 4, 5])
   a1

Out[4]:
   array([1, 2, 3, 4, 5])

In [5]:
   # what is its type?
   type(a1)

Out[5]:
   numpy.ndarray

In [6]:
   # how many elements?
   np.size(a1)

Out[6]:
   5

In NumPy, n-dimensional arrays are denoted as ndarray, and this one contains five elements, as is reported by the np.size() function.

NumPy arrays must have all of their elements of the same type. If you specify different types in the list, NumPy will try to coerce all the items to the same type. The following code example demonstrates using integer and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required