@@ -2063,57 +2063,77 @@ def __delitem__(self, key):
2063
2063
2064
2064
def take (self , indices , axis = 0 , convert = True , is_copy = True , ** kwargs ):
2065
2065
"""
2066
- Return an object formed from the elements in the given indices along an
2067
- axis
2066
+ Return the elements in the given *positional* indices along an axis.
2067
+
2068
+ This means that we are not indexing according to actual values in
2069
+ the index attribute of the object. We are indexing according to the
2070
+ actual position of the element in the object.
2068
2071
2069
2072
Parameters
2070
2073
----------
2071
- indices : list / array of ints
2074
+ indices : array-like
2075
+ An array of ints indicating which positions to take.
2072
2076
axis : int, default 0
2073
- convert : translate neg to pos indices (default)
2074
- is_copy : mark the returned frame as a copy
2077
+ The axis on which to select elements. "0" means that we are
2078
+ selecting rows, "1" means that we are selecting columns, etc.
2079
+ convert : bool, default True
2080
+ Whether to convert negative indices to positive ones, just as with
2081
+ indexing into Python lists. For example, if `-1` was passed in,
2082
+ this index would be converted ``n - 1``
2083
+ is_copy : bool, default True
2084
+ Whether to return a copy of the original object or not.
2075
2085
2076
2086
Examples
2077
2087
--------
2078
- >>> import numpy as np
2079
- >>> import pandas as pd
2080
2088
>>> df = pd.DataFrame([('falcon', 'bird', 389.0),
2081
2089
('parrot', 'bird', 24.0),
2082
2090
('lion', 'mammal', 80.5),
2083
2091
('monkey', 'mammal', np.nan)],
2084
- columns=('name', 'class', 'max_speed'))
2092
+ columns=('name', 'class', 'max_speed'),
2093
+ index=[0, 2, 3, 1])
2085
2094
>>> df
2086
2095
name class max_speed
2087
2096
0 falcon bird 389.0
2088
- 1 parrot bird 24.0
2089
- 2 lion mammal 80.5
2090
- 3 monkey mammal NaN
2097
+ 2 parrot bird 24.0
2098
+ 3 lion mammal 80.5
2099
+ 1 monkey mammal NaN
2100
+
2101
+ Take elements at positions 0 and 3 along the axis 0 (default).
2091
2102
2092
- Take elements at indices 0 and 3 along the axis 0 (default)
2103
+ Note how the actual indices selected (0 and 1) do not correspond to
2104
+ our selected indices 0 and 3. That's because we are selecting the 0th
2105
+ and 3rd rows, not rows whose indices equal 0 and 3.
2093
2106
2094
2107
>>> df.take([0, 3])
2095
2108
0 falcon bird 389.0
2096
- 3 monkey mammal NaN
2109
+ 1 monkey mammal NaN
2097
2110
2098
- Take elements at indices 1 and 2 along the axis 1
2111
+ Take elements at indices 1 and 2 along the axis 1 (column selection).
2099
2112
2100
2113
>>> df.take([1, 2], axis=1)
2101
2114
class max_speed
2102
2115
0 bird 389.0
2103
- 1 bird 24.0
2104
- 2 mammal 80.5
2105
- 3 mammal NaN
2116
+ 2 bird 24.0
2117
+ 3 mammal 80.5
2118
+ 1 mammal NaN
2106
2119
2107
- Also, we may take elements using negative integers for pos indices
2120
+ We may take elements using negative integers for positive indices,
2121
+ starting from the end of the object, just like with Python lists.
2108
2122
2109
2123
>>> df.take([-1, -2])
2110
2124
name class max_speed
2111
- 3 monkey mammal NaN
2112
- 2 lion mammal 80.5
2125
+ 1 monkey mammal NaN
2126
+ 3 lion mammal 80.5
2113
2127
2114
2128
Returns
2115
2129
-------
2116
2130
taken : type of caller
2131
+ An array-like containing the elements taken from the object.
2132
+
2133
+ See Also
2134
+ --------
2135
+ numpy.ndarray.take
2136
+ numpy.take
2117
2137
"""
2118
2138
nv .validate_take (tuple (), kwargs )
2119
2139
self ._consolidate_inplace ()
0 commit comments