Convert pandas DataFrame Index to List & NumPy Array in Python (2 Examples)
This article illustrates how to get the indices of a pandas DataFrame as a list or a NumPy array in the Python programming language.
The article will consist of two examples for the extraction of the indices of a pandas DataFrame as a list or a NumPy array. To be more specific, the content looks as follows:
Let’s jump right to the programming part.
Example Data & Libraries
In order to use the functions of the pandas library, we first need to load pandas:
import pandas as pd # Import pandas library
The following data is used as a basis for this Python programming tutorial:
data = pd.DataFrame({"x1":range(1, 10), # Create pandas DataFrame "x2":["a", "b", "c", "d", "e", "d", "c", "b", "a"], "x3":range(10, 1, - 1)}) print(data) # Print pandas DataFrame
Table 1 shows the structure of the example DataFrame: It consists of nine rows and three columns and the index names are ranging from 0 to 8.
Example 1: Convert pandas DataFrame Index to List
Example 1 demonstrates how to extract the index names of a pandas DataFrame as a list object in Python.
To achieve this, we can use the index attribute and the tolist function as shown in the following Python syntax:
data_index1 = data.index.tolist() # Extract as list object print(data_index1) # Print index as list # [0, 1, 2, 3, 4, 5, 6, 7, 8]
Have a look at the output of the previous Python code: We have created a new list object containing the index values of our input DataFrame.
Example 2: Convert pandas DataFrame Index to NumPy Array
Example 2 explains how to transform the index values of a pandas DataFrame to a NumPy array.
To return the indices as a NumPy array, we have to use the index.values attribute as shown below:
data_index2 = data.index.values # Extract as NumPy array print(data_index2) # Print index as NumPy array # [0 1 2 3 4 5 6 7 8]
The previous output shows that we have created a new NumPy array containing the index range of our data set.
Video & Further Resources
I have recently published a video on my YouTube channel, which demonstrates the Python code of the present article. You can find the video below.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you could read the related Python articles on my website. Some tutorials are listed below.
- Handling DataFrames Using the pandas Library in Python
- Get pandas DataFrame Column as List in Python
- Get Index of Column in pandas DataFrame in Python
- Get Column Names of pandas DataFrame as List in Python
- Convert pandas DataFrame to NumPy Array in Python
- Introduction to Lists in Python
- All Python Programming Tutorials
Summary: At this point in the article you should have learned how to extract the index names of a pandas DataFrame as a list or a NumPy array in the Python programming language. Tell me about it in the comments, if you have further questions.