Chapter 3. NumPy for pandas

Numerical Python (NumPy) is an open source Python library for scientific computing. NumPy provides a host of features that allow a Python programmer to work with high-performance arrays and matrices. NumPy arrays are stored more efficiently than Python lists and allow mathematical operations to be vectorized, which results in significantly higher performance than with looping constructs in Python.

pandas builds upon functionality provided by NumPy. The pandas library relies heavily on the NumPy array for the implementation of the pandas Series and DataFrame objects, and shares many of its features such as being able to slice elements and perform vectorized operations. It is therefore useful to spend some time going over NumPy arrays before diving into pandas.

In this chapter, we will cover the following topics about NumPy arrays:

  • Installing and importing NumPy
  • Benefits and characteristics of NumPy arrays
  • Creating NumPy arrays and performing basic array operations
  • Selecting array elements
  • Logical operation on arrays
  • Slicing arrays
  • Reshaping arrays
  • Combining arrays
  • Splitting arrays
  • Useful numerical methods of NumPy arrays

Installing and importing NumPy

Since NumPy is a prerequisite for pandas, and you have already installed pandas, NumPy is ready to be used. All that is required to do to use NumPy is to import the library and so all that is required for the examples in this chapter and for most of this book is the following import command:

In [1]:
   # this allows us to access numpy using the
   # np. prefix
   import numpy as np

This makes the top-level functions of NumPy available in the np namespace. This is a common practice when using NumPy, and this book will follow this convention for accessing NumPy functionality.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.109.8