How to Use the take() Function in Pandas


Often you may want to return the elements in a specific position along an axis in a pandas DataFrame.

The easiest way to do so is by using the take() function, which performs this exact task.

The take() function uses the following syntax:

pandas.DataFrame.take(indices, axis=0, …)

where:

  • indices: An array of integers indicating which positions to take
  • axis: The axis on which to select elements (0 = rows, 1 = columns)

The following example shows how to use the take() function in practice with a pandas DataFrame.

Example: How to Use the take() Function in Pandas

Suppose we create the following pandas DataFrame that contains information about various basketball players, including the team they play for and their average points scored per game:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Magic', 'Heat', 'mavs', 'Nets', 'hawks'],
                   'points': [29, 34, 12, 15, 22, 40]})

#view DataFrame
print(df)

    team  points
0   Mavs      29
1  Magic      34
2   Heat      12
3   mavs      15
4   Nets      22
5  hawks      40

Suppose that we would like to extract the element located in index position 1 along the rows of the DataFrame.

We can use the take() function with the following syntax to do so:

#extract element located in index position 1 along rows
df.take([1])

         team	points
1	Magic	34

Notice that this returns the entire row located in index position 1 along the rows of the DataFrame.

Also note that we can specify multiple indices to extract along the rows of the DataFrame.

For example, we can use the following syntax with the take() function to extract the rows with index values 1, 2, 4 and 5 of the DataFrame:

#extract element located in index positions 1, 2, 4, 5 along rows
df.take([1, 2, 4, 5]) 

         team	points
1	Magic	34
2	Heat	12
4	Nets	22
5	hawks	40

Notice that this returns all of the rows whose indices we specified in the take() function.

We can also specify negative values for the indices to instead extract rows based on their position relative to the last row.

For example, we can use the following syntax with the take() function to extract the rows that are first and third from last in the DataFrame:

#extract element located in index positions -1 and -3
df.take([-1, -3])

team	points
5	hawks	40
3	mavs	15

We can see that this returns the rows that are first and third from last in the DataFrame.

Also note that we could instead use the take() function to extract columns based on their index position by using the argument axis=1.

For example, we can use the following syntax to extract the column located in index position 1 along the columns of the DataFrame:

#extract element located in index positions 1 along columns
df.take([1], axis=1)

points
0	29
1	34
2	12
3	15
4	22
5	40

Notice that this returns all values from the points column of the DataFrame, which represents the column located at index position 1.

Note that index positions for both the rows and columns of a DataFrame start at 0.

Note: You can find the complete documentation for the take() function in pandas here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Use the Rolling.apply() Function in Pandas
How to Use the nunique() Function in Pandas
How to Use the get_loc() Function in Pandas
How to Use idxmin() Function in Pandas

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *