- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.take#
- DataFrame.take(indices,axis=0,**kwargs)[source]#
Return the elements in the givenpositional indices along an axis.
This means that we are not indexing according to actual values inthe index attribute of the object. We are indexing according to theactual position of the element in the object.
- Parameters:
- indicesarray-like
An array of ints indicating which positions to take.
- axis{0 or ‘index’, 1 or ‘columns’, None}, default 0
The axis on which to select elements.
0
means that we areselecting rows,1
means that we are selecting columns.ForSeries this parameter is unused and defaults to 0.- **kwargs
For compatibility with
numpy.take()
. Has no effect on theoutput.
- Returns:
- same 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
>>>df=pd.DataFrame([('falcon','bird',389.0),...('parrot','bird',24.0),...('lion','mammal',80.5),...('monkey','mammal',np.nan)],...columns=['name','class','max_speed'],...index=[0,2,3,1])>>>df name class max_speed0 falcon bird 389.02 parrot bird 24.03 lion mammal 80.51 monkey mammal NaN
Take elements at positions 0 and 3 along the axis 0 (default).
Note how the actual indices selected (0 and 1) do not correspond toour selected indices 0 and 3. That’s because we are selecting the 0thand 3rd rows, not rows whose indices equal 0 and 3.
>>>df.take([0,3]) name class max_speed0 falcon bird 389.01 monkey mammal NaN
Take elements at indices 1 and 2 along the axis 1 (column selection).
>>>df.take([1,2],axis=1) class max_speed0 bird 389.02 bird 24.03 mammal 80.51 mammal NaN
We may take elements using negative integers for positive indices,starting from the end of the object, just like with Python lists.
>>>df.take([-1,-2]) name class max_speed1 monkey mammal NaN3 lion mammal 80.5