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 DataFrame convert pandas dataframe index list numpy array python

 

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.

 

 

In addition, you could read the related Python articles on my website. Some tutorials are listed below.

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top