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 slice syntax is identical to that in NumPy arrays. The following example selects rows from the Series
by position starting from and including 0
, up to but not inclusive of 6
, and stepping...