• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get pandas Index Values as List in Python

Get pandas Index Values as List in Python

October 14, 2022 Leave a Comment

To get the index of a pandas DataFrame and create a list in Python, the easiest way by using index.to_list() on a DataFrame.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

index_list = df.index.to_list()

print(index_list)

#Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8]

You can also use index.values.tolist() function to convert a pandas Index to a list.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

index_list = df.index.values.tolist()

print(index_list)

#Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8]

When working with collections of data, the ability to be able to easily access certain pieces of information is valuable.

One such situation is if you want to get the index values of a pandas DataFrame in Python.

There are a few ways you can convert the index of a pandas DataFrame to a list.

Having the index values in a list can be useful if you want to loop over the index values and perform an action.

The easiest way is to access the index with the pandas DataFrame index attribute and then use the to_list() function.

Below shows a simple example of how you can convert a DataFrame index to list in Python.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

index_list = df.index.to_list()

print(index_list)

#Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Using index.values.tolist() to Convert pandas Index to List in Python

Another way you can convert index values to a list is with the index.values.tolist() function.

Below shows another example of how you can get the index of a DataFrame as a list in Python.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

index_list = df.index.values.tolist()

print(index_list)

#Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Hopefully this article has been useful for you to be able to learn how to convert pandas Index to a list in Python.

Other Articles You'll Also Like:

  • 1.  Count Number of Keys in Dictionary in Python
  • 2.  Python Infinity – How to Use Infinity in Python
  • 3.  Using Lambda Expression with max() in Python
  • 4.  pandas mode – Find Mode of Series or Columns in DataFrame
  • 5.  islower Python – Check if All Letters in String Are Lowercase
  • 6.  Python Trig – Using Trigonometric Functions in Python for Trigonometry
  • 7.  How to Check if List is Empty in Python
  • 8.  Remove First and Last Character from String Using Python
  • 9.  Calculate Sum of Dictionary Values in Python
  • 10.  Create List of Odd Numbers in Range with Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x