Book Image

Learning Pandas

By : Michael Heydt
Book Image

Learning Pandas

By: Michael Heydt

Overview of this book

Table of Contents (19 chapters)
Learning pandas
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Slicing arrays


NumPy arrays support a feature called slicing. Slicing retrieves zero or more items from an array, and the items also don't need to be sequential, whereas the normal array element operator [] can only retrieve one value. This is very convenient as it provides an ability to efficiently select multiple items from an array without the need to implement Python loops.

Slicing overloads the normal array [] operator to accept what is referred to as a slice object. A slice object is created using a syntax of start:end:step. Each component of the slice is optional and, as we will see, this provides convenient means to select entire rows or columns by omitting the component of the slice.

To begin with the demonstrations, the following code creates a ten-element array and selects items in zero-based positions from 3 up to, but not including, position 8:

In [36]:
   # get all items in the array from position 3
   # up to position 8 (but not inclusive)
   a1 = np.arange(1, 10)
   a1[3:8...