pyspark.pandas.Series.take

Series.take(indices: Sequence[int]) → IndexOpsLike

Return the elements in the given positional indices along an axis.

This means that we are not indexing according to actual values in the index attribute of the object. We are indexing according to the actual position of the element in the object.

Parameters
indicesarray-like

An array of ints indicating which positions to take.

Returns
takensame type as caller

An array-like containing the elements taken from the object.

See also

DataFrame.loc

Select a subset of a DataFrame by labels.

DataFrame.iloc

Select a subset of a DataFrame by positions.

numpy.take

Take elements from an array along an axis.

Examples

Series

>>> psser = ps.Series([100, 200, 300, 400, 500])
>>> psser
0    100
1    200
2    300
3    400
4    500
dtype: int64
>>> psser.take([0, 2, 4]).sort_index()
0    100
2    300
4    500
dtype: int64

Index

>>> psidx = ps.Index([100, 200, 300, 400, 500])
>>> psidx  
Int64Index([100, 200, 300, 400, 500], dtype='int64')
>>> psidx.take([0, 2, 4]).sort_values()  
Int64Index([100, 300, 500], dtype='int64')

MultiIndex

>>> psmidx = ps.MultiIndex.from_tuples([("x", "a"), ("x", "b"), ("x", "c")])
>>> psmidx  
MultiIndex([('x', 'a'),
            ('x', 'b'),
            ('x', 'c')],
           )
>>> psmidx.take([0, 2])  
MultiIndex([('x', 'a'),
            ('x', 'c')],
           )