Pandas DataFrame take() Method
Last Updated :
01 Feb, 2024
Python is a great tool for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages like Pandas which make analyzing data much easier.
Pandas take() function returns elements on the given 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.
Example:
Python3
import pandas as pd
import numpy as np
df = pd.DataFrame([
('eagle', 'bird', 320.0),
('sparrow', 'bird', 18.0),
('tiger', 'mammal', 80.5),
('monkey', 'mammal', np.nan)
], columns=['name', 'class', 'max_speed'], index=[0, 2, 3, 1])
df.take([0, 3])
Output:
name class max_speed
0 eagle bird 320.0
1 monkey mammal NaN
Syntax
Syntax: DataFrame.take(indices, axis=0, **kwargs)
Parameters
- indices: An array of ints indicating which positions to take.
- axis: The axis on which to select elements. 0 means that we are selecting rows, and 1 means that we are selecting columns
- **kwargs: Used for compatibility with numpy.take(). Does not affect the output.
Returns
- An array-like containing the elements taken from the object.
Download the sample CSV file here to practice DataFrame.take() method.
Examples
Let's understand how to use take() method of the Pandas library to get elements in the given positional indices along an axis in a DataFrame.
Example 1: Use the DataFrame.take() function over the index(row) axis.
We will cover using DataFrame.take() the method keeping the axis parameter as "0".
This will extract data row-wise from the DataFrame.
First, let's create a DataFrame.
Python3
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.read_csv("nba.csv")
# Print the dataframe
df

Now we will modify the index labels for demonstration purposes. Right now the labels are numbered from 0 to 914.
Python3
# double the value of index labels
df.index = df.index * 2
# Print the modified dataframe
df

Let's take the values at positions 0, 1, and 2 using DataFrame.take() function
Python3
# take values at input position over the index axis
df.take([0, 1, 2], axis = 0)
Output :

As we can see in the output, the values are selected based on the position but not on the index labels.
Example 2: Use the DataFrame.take() function over the column axis.
This will be very similar to Example 1, we just need to change axis parameter value to 1.
First, lets create a DataFrame
Python3
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.read_csv("nba.csv")
# Print the dataframe
df

Now we will take values at positions 0, 1, and 2 over the column axis.
Python3
# take values over the column axis.
df.take([0, 1, 2], axis = 1)
Output :

Conclusion
The DataFrame.take() method is a built-in function of the Pandas library in Python and is a very useful function used for data manipulation. It lets you extract specific data based on their position and returns a new DataFrame containing the extracted data.
This tutorial is made for the latest version of Python, teaching you how to use DataFrame.take() method. We have discussed the DataFrame.take() method with examples in easy language.
Similar Reads
Pandas DataFrame.to_sparse() Method
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure o
2 min read
Pandas DataFrame.loc[] Method
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure o
6 min read
Pandas DataFrame drop() Method
Drop is a useful functionality in Pandas used to remove specified labels from rows or columns in a DataFrame and it provides options to modify the original DataFrame directly or return a new one with the changes. Since drop works for both columns and rows we have to specify the axis. By default, the
4 min read
Pandas dataframe.groupby() Method
Pandas groupby() function is a powerful tool used to split a DataFrame into groups based on one or more columns, allowing for efficient data analysis and aggregation. It follows a "split-apply-combine" strategy, where data is divided into groups, a function is applied to each group, and the results
6 min read
Pandas DataFrame iterrows() Method
iterrows() method in Pandas is a simple way to iterate over rows of a DataFrame. It returns an iterator that yields each row as a tuple containing the index and the row data (as a Pandas Series). This method is often used in scenarios where row-wise operations or transformations are required. Exampl
4 min read
Get Size of the Pandas DataFrame
In this article, we will discuss how to get the size of the Pandas Dataframe using Python. Method 1 : Using df.size This will return the size of dataframe  i.e. rows*columns Syntax: dataframe.size where, dataframe is the input dataframe Example: Python code to create a student dataframe and display
2 min read
Python | Pandas dataframe.get()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.get() function is used to get item from object for given key. The key
2 min read
Methods to Round Values in Pandas DataFrame
There are various ways to Round Values in Pandas DataFrame so let's see each one by one: Let's create a Dataframe with 'Data Entry' Column only: Code: Python3 # import Dataframe class # from pandas library from pandas import DataFrame # import numpy library import numpy as np # dictionary Myvalue =
3 min read
Pandas DataFrame itertuples() Method
itertuples() is a method that is used to iterate over the rows and return the values along with attributes in tuple format. It returns each row as a lightweight namedtuple, which is faster and more memory-efficient than other row iteration methods like iterrows(). Let us consider one sample example.
7 min read
Python | Pandas Dataframe/Series.head() method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas head() method is used to return top n (5 by default) rows of a data frame or se
2 min read