Python | Pandas Series.take ()

NumPy | Python Methods and Functions

Series.take() Pandas Series.take() returns items at given positional indices along the axis ... Here we are not indexing according to the actual values ​​in the index attribute of the object. We index according to the actual position of the element in the object.

Syntax: Series.take (indices, axis = 0, convert = None, is_copy = True, ** kwargs)

Parameter:
indices: An array of ints indicating which positions to take.
axis: The axis on which to select elements.
- & gt;  0 means that we are selecting rows.
- & gt;  1 means that we are selecting columns.
convert: Whether to convert negative indices into positive ones
is_copy: Whether to return a copy of the original object or not.
** kwargs: For compatibility with numpy.take (). Has no effect on the output.

Returns: taken: same type as caller

Example # 1: Use Series.take () to extract some elements from a given series object based on the actual position of the elements in the object.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ `New York` , `Chicago` , ` Toronto` , `Lisbon` , `Rio` , ` Moscow` ])

  
# Create date and time index

didx = pd.DatetimeIndex (start = `2014-08-01 10:00` , freq = `W`

periods = 6 , tz = `Europe / Berlin`

 
# set index

sr.index = didx

 
# Print series

print (sr)

Output:

We will now use Series.take () to retrieve the values ​​corresponding to the traversed positions.

# return items that match
# index position passed

sr.take (indices = [ 0 , 2 ])

Output:

As we can see in the output, Series.take () successfully returned elements matching the passed index positions of this series object.

Example # 2: Use Series.take () to extract some elements from a given series object based on the actual position of the elements in the object.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ 19.5 , 16.8 , None , 22.78 , None , 20.124 , None , 18.1002 , None ])

 
# Print series

print (sr)

Output:

We will now use Series.take () to retrieve values ​​corresponding to the positions passed.

 

# return elements that match
# index position passed

sr.take (indices = [ 1 , 2 , 5 , 8 ])

Output:

As we can see in the output, Series.take () successfully returned elements matching the passed index positions of this series object.





Get Solution for free from DataCamp guru