Table of Contents
Converting a Pandas Index to a list involves transforming the index labels of a Pandas dataframe or series into a Python list object. This can be achieved by using the tolist() method, which converts the index values into a list and returns it. This conversion can be useful for various data manipulation and analysis tasks, as it allows for easier indexing and iteration through the data. Additionally, the resulting list can be further manipulated and used in other programming functions.
Convert Pandas Index to a List (With Examples)
You can use one of the following two methods to convert the index of a pandas DataFrame to a list:
Method 1: Use list()
index_list = list(df.index.values)
Method 2: Use tolist()
index_list = df.index.values.tolist()
Both methods will return the exact same result, but the second method will tend to be faster on extremely large DataFrames.
The following examples show how to use each method with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], 'points': [18, 22, 19, 14, 14, 11, 20, 28], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame df team points assists rebounds 0 A 18 5 11 1 B 22 7 8 2 C 19 7 10 3 D 14 9 6 4 E 14 12 6 5 F 11 9 5 6 G 20 9 9 7 H 28 4 12
Method 1: Use list()
The following code shows how to use list() to quickly convert the index of the pandas DataFrame to a list:
#convert index to list
index_list = list(df.index.values)
#view list
index_list
[0, 1, 2, 3, 4, 5, 6, 7]
Notice that the list contains the original values in the index of the DataFrame.
We can also use type() to verify that the result is a list:
#check object type
type(index_list)
listMethod 2: Use tolist()
The following code shows how to use list() to quickly convert the index of the pandas DataFrame to a list:
#convert index to list
index_list = df.index.values.tolist()
#view list
index_list
[0, 1, 2, 3, 4, 5, 6, 7]
We can also use type() to verify that the result is a list:
#check object type
type(index_list)
listAdditional Resources
The following tutorials explain how to perform other common operations in pandas:
Cite this article
stats writer (2024). How can I convert a Pandas Index to a list?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-pandas-index-to-a-list/
stats writer. "How can I convert a Pandas Index to a list?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-pandas-index-to-a-list/.
stats writer. "How can I convert a Pandas Index to a list?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-pandas-index-to-a-list/.
stats writer (2024) 'How can I convert a Pandas Index to a list?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-pandas-index-to-a-list/.
[1] stats writer, "How can I convert a Pandas Index to a list?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I convert a Pandas Index to a list?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
