Convert Pandas Index to List

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:February 6, 2023

To convert an index to a list in Pandas DataFrame, use the Index.tolist(), Index.values.tolist() and list() functions. These functions are used to return a list of values. In this article, I will explain how to convert the pandas DataFrame index to a list by using list(), index.tolist(), and index.values functions with examples.

1. Quick Examples of Convert Index to List

If you are in a hurry, below are some quick examples of how to convert the index to list in DataFrame.


# Below are some quick examples 

# Example 1: Use tolist() function to convert index to list
df2 = df.index.tolist()

# Example 2: Convert the index as list using tolist()
df2 = df.index.values.tolist()

# Example 3: Convert the index into a list
df2 = list(df.index)

# Example 4: Use list() function to convert index as a list
df2 = list(df.index.values)

2. Syntax of Index.tolist()

Following is the syntax of Pandas Index.tolist() function.


# Syntax of Index.tolist()
Index.tolist()

It returns a list of the values.

Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are CoursesFeeDuration and Discount.


import pandas as pd
import numpy as np
technologies= ({
    'Courses':["Spark","PySpark","Hadoop","Pandas"],
    'Fee' :['22000','25000','24000','26000'],
    'Duration':['30days','50days','40days','60days'],
    'Discount':['1000','2300','2500','1400']
              })
df = pd.DataFrame(technologies, index = ['r1', 'r2', 'r3', 'r4'])
print(df)

Yields below output.


# Output:
    Courses    Fee Duration Discount
r1    Spark  22000   30days     1000
r2  PySpark  25000   50days     2300
r3   Hadoop  24000   40days     2500
r4   Pandas  26000   60days     1400

3. Pandas Convert Index to List using tolist()

We can use the Pandas DataFrame.index.tolist() function to convert a DataFrame index to a list object. pandas.Index is a basic object that stores axis labels for all pandas objects. The Index is a type of class pandas.core.indexes.base.Index


# Use tolist() function to convert index to list
df2 = df.index.tolist()
print(df2)

Yields below output.


# Output:
['r1', 'r2', 'r3', 'r4']

Alternatively, you can also use df.index.values.tolist().


# Using df.index.values
df.index.values.tolist()

4. Convert Index as a List Using list()

You can also use the Python list() function to convert the pandas index to a list object. This function takes an object as an argument you wanted to convert, I am using DataFrame.Index object as an argument.


# Use list() function to convert index as a list
df2 = list(df.index)
print(df2)

Yields the same output as above.

We could also pass DataFrame.index.values as a param. index.values actually return a NumPy object, so here we are actually converting NumPy to List.


# Convert the index into a list
df2 = list(df.index.values)
print(df2)

Yields the same output as above.

6. Complete Example For Convert Index as a List


import pandas as pd
import numpy as np
technologies= ({
    'Courses':["Spark","PySpark","Hadoop","Pandas"],
    'Fee' :['22000','25000','24000','26000'],
    'Duration':['30days','50days','40days','60days'],
    'Discount':['1000','2300','2500','1400']
              })
df = pd.DataFrame(technologies, index = ['r1', 'r2', 'r3', 'r4'])
print(df)

# # Use tolist() function to convert index to list
df2 = df.index.tolist()
print(df2)

# convert the index as list using tolist()
df2 = df.index.values.tolist()
print(df2)

# Convert the index into a list
df2 = list(df.index)
print(df2)

# Use list() function to convert index as a list
df2 = list(df.index.values)
print(df2)

7. Conclusion

In this article, I have explained how to convert an index to a list in Pandas DataFrame by using Index.tolist(), list(), and index.values.tolist() function with examples.

Happy Learning !!

Related Articles

Leave a Reply

You are currently viewing Convert Pandas Index to List