Site icon Spark By {Examples}

Convert Pandas Index to List

pandas index to list

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.

Key Points –

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 a list in DataFrame.


# Quick examples of convert index to list 

# 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 the 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. 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)

In the above example, df.index.tolist() is used to convert the DataFrame’s index to a Python list. When you run this code, it will print the list of index values: ['r1', 'r2', 'r3', 'r4']. This example yields the below output.


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

Alternatively, another approach to convert the index of a Pandas DataFrame to a list is by using the values attribute along with tolist().


# Using df.index.values
df2 = df.index.values.tolist()
print(df2)

This program achieves the same result as df.index.tolist() and will print the list of index values: ['r1', 'r2', 'r3', 'r4']. The values attribute returns a NumPy array, and tolist() converts it to a Python list.

4. Convert the 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 want 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)

In the above example, list(df.index) is used to convert the DataFrame’s index to a Python list. When you run this code, it will print the list of index values: ['r1', 'r2', 'r3', 'r4']. Yields the same output as above.

Similarly, 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. It uses the list() function along with the values attribute to convert the index of the Pandas DataFrame to a Python 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)

Frequently Asked Questions on Convert Index to List

How can I convert the index of a Pandas DataFrame or Series to a list in Python?

You can convert the index of a Pandas DataFrame or Series to a list in Python using the tolist() method or by using the list() function.

Can I convert the index of a Series to a list in a similar way?

You can absolutely convert the index of a Pandas Series to a list in a similar way as you would for a DataFrame. You can use either the tolist() method or the list() function.

Is there an alternative method to convert the index to a list?

There is an alternative method to convert the index of a Pandas DataFrame or Series to a list using the tolist() or list() function. Another approach is to use the built-in list() constructor directly on the index object.

What if I want to reset the index before converting it to a list?

If you want to reset the index of a Pandas DataFrame or Series before converting it to a list, you can use the reset_index() method, which brings the current index into a new column and assigns a default integer index. After resetting the index, you can then use the tolist() or list() function to convert the new index to a list.

Can I convert the index to a list without using any Pandas methods?

You can convert the index to a list without using any Pandas methods. You can directly use the list() function on the Pandas DataFrame or Series index.

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

References

Exit mobile version