Slicing a Series

In Chapter 3, NumPy for pandas, we covered techniques for NumPy array slicing. pandas Series objects also support slicing and override the slicing operators to perform their magic on Series data. Just like NumPy arrays, you can pass a slice object to the [] operator of the Series to get the specified values. Slices also work with the .loc[], .iloc[], and .ix properties and accessors.

To demonstrate slicing, we will use the following Series:

In [83]:
   # a Series to use for slicing
   # using index labels not starting at 0 to demonstrate
   # position based slicing
   s = pd.Series(np.arange(100, 110), index=np.arange(10, 20))
   s

Out[83]:
   10    100
   11    101
   12    102
   13    103
   14    104
   15    105
   16    106
   17    107
   18    108
   19    109
   dtype: int64

The ...

Get Learning pandas 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.