w3resource

Pandas Series: take() function

Series-take() function

The take() function is used to 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.

Syntax:

Series.take(self, indices, axis=0, is_copy=False, **kwargs)
Pandas Series take image
Name Description Type/Default Value Required / Optional
indices An array of ints indicating which positions to take. array-like Required
axis The axis on which to select elements. 0 means that we are selecting rows, 1 means that we are selecting columns. {0 or ‘index’, 1 or ‘columns’, None}
Default Value: 0
Required
is_copy Whether to return a copy of the original object or not. bool
Default Value: True
Required
**kwargs For compatibility with numpy.take(). Has no effect on the output Required

Returns: taken - same type as caller An array-like containing the elements taken from the object.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([('Eagle', 'bird',    200.0),
                   ('sparrow', 'bird',     22.0),
                   ('tiger',   'mammal',   120.5),
                   ('fox', 'mammal', np.nan)],
                   columns=['name', 'class', 'max_speed'],
                   index=[0, 1, 2, 3])
df

Output:

  name	class	max_speed
0	Eagle	bird	200.0
1	sparrow	bird	22.0
2	tiger	mammal	120.5
3	fox	    mammal	NaN
Pandas Series take image

Example - Take elements at positions 0 and 3 along the axis 0 (default):

Note how the actual indices selected (0 and 1) do not correspond to our selected indices 0 and 3. That’s because we are selecting the 0th and 3rd rows, not rows whose indices equal 0 and 3.

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([('Eagle', 'bird',    200.0),
                   ('sparrow', 'bird',     22.0),
                   ('tiger',   'mammal',   120.5),
                   ('fox', 'mammal', np.nan)],
                   columns=['name', 'class', 'max_speed'],
                   index=[0, 1, 2, 3])
df.take([0, 3])

Output:

  name	class	max_speed
0	Eagle	bird	200.0
3	fox	   mammal	 NaN

Example - Take elements at indices 1 and 2 along the axis 1 (column selection):

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([('Eagle', 'bird',    200.0),
                   ('sparrow', 'bird',     22.0),
                   ('tiger',   'mammal',   120.5),
                   ('fox', 'mammal', np.nan)],
                   columns=['name', 'class', 'max_speed'],
                   index=[0, 1, 2, 3])
df.take([1, 2], axis=1)

Output:

  class	max_speed
0	bird	200.0
1	bird	22.0
2	mammal	120.5
3	mammal	NaN

We may take elements using negative integers for positive indices, starting from the end of the object, just like with Python lists.

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([('Eagle', 'bird',    200.0),
                   ('sparrow', 'bird',     22.0),
                   ('tiger',   'mammal',   120.5),
                   ('fox', 'mammal', np.nan)],
                   columns=['name', 'class', 'max_speed'],
                   index=[0, 1, 2, 3])
df.take([-1, -2])

Output:

  name  class	 max_speed
3	fox	  mammal	  NaN
2	tiger mammal	120.5

Previous: Series-set_axis() function
Next: Series-tail() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-take.php