pandas.core.groupby.SeriesGroupBy.take#
- SeriesGroupBy.take(indices, axis=0, **kwargs)[source]#
Return the elements in the given positional indices in each group.
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.
If a requested index does not exist for some group, this method will raise. To get similar behavior that ignores indices that don’t exist, see
SeriesGroupBy.nth()
.- Parameters
- indicesarray-like
An array of ints indicating which positions to take in each group.
- axis{0 or ‘index’, 1 or ‘columns’, None}, default 0
The axis on which to select elements.
0
means that we are selecting rows,1
means that we are selecting columns. For SeriesGroupBy this parameter is unused and defaults to 0.- **kwargs
For compatibility with
numpy.take()
. Has no effect on the output.
- Returns
- Series
A Series containing the elements taken from each group.
See also
Series.take
Take elements from a Series along an axis.
Series.loc
Select a subset of a DataFrame by labels.
Series.iloc
Select a subset of a DataFrame by positions.
numpy.take
Take elements from an array along an axis.
SeriesGroupBy.nth
Similar to take, won’t raise if indices don’t exist.
Examples
>>> df = pd.DataFrame([('falcon', 'bird', 389.0), ... ('parrot', 'bird', 24.0), ... ('lion', 'mammal', 80.5), ... ('monkey', 'mammal', np.nan), ... ('rabbit', 'mammal', 15.0)], ... columns=['name', 'class', 'max_speed'], ... index=[4, 3, 2, 1, 0]) >>> df name class max_speed 4 falcon bird 389.0 3 parrot bird 24.0 2 lion mammal 80.5 1 monkey mammal NaN 0 rabbit mammal 15.0 >>> gb = df["name"].groupby([1, 1, 2, 2, 2])
Take elements at positions 0 and 1 along the axis 0 in each group (default).
>>> gb.take([0, 1]) 1 4 falcon 3 parrot 2 2 lion 1 monkey Name: name, dtype: object
We may take elements using negative integers for positive indices, starting from the end of the object, just like with Python lists.
>>> gb.take([-1, -2]) 1 3 parrot 4 falcon 2 0 rabbit 1 monkey Name: name, dtype: object