How to Get the Index of a Dataframe in Python Pandas?

How To Get The Index Of The DataFrame

Hello folks! In this tutorial, we are going to discuss the different ways to get the index or rows of a pandas DataFrame object. So, let’s get started.


Methods to Get Index of a Dataframe in Python

Let’s get right into the steps to find the index of a dataframe. Also, check out how you can reset the index of a dataframe to ensure that everytime you append, or sort a dataframe, the index numbers are aligned.

Method 1: Using for loop

In Python, we can easily get the index or rows of a pandas DataFrame object using a for loop. In this method, we will create a pandas DataFrame object from a Python dictionary using the pd.DataFrame() function of pandas module in Python. Then we will run a for loop over the pandas DataFrame index object to print the index. Let’s implement this through Python code.

# Method-1

# Import pandas
import pandas as pd

# Create a Python dictionary
data = {"Name": ['Sanjay', 'Shreya', 'Raju', 'Gopal', 'Ravi'],
        "Roll": [101, 102, 103, 104, 105]}

# Create a DataFrame object from above dictionary
df = pd.DataFrame(data, index = [1, 2, 3, 4, 5])
print("This is DataFrame:\n")
print(df)

# Get the index/rows of the above DataFrame 
# Using for loop iteration
print("\nThis is index of DataFrame:\n")
for idx in df.index:
    print(idx, end = ' ')

Output:

This is DataFrame:

     Name  Roll
1  Sanjay   101
2  Shreya   102
3    Raju   103
4   Gopal   104
5    Ravi   105

This is index of DataFrame:

1 2 3 4 5

Method 2: Using index attribute

This is the most widely used method to get the index of a DataFrame object. In this method, we will be creating a pandas DataFrame object using the pd.DataFrame() function of as usual. Then we will use the index attribute of pandas DataFrame class to get the index of the pandas DataFrame object. As we apply the index attribute on the pandas DataFrame object, it returns a tuple that contains the index list of the DataFrame. Let’s see how we can actually implement this in Python programming.

# Method-2

# Import pandas
import pandas as pd

# Create a Python dictionary
data = {"Name": ['Sanjay', 'Shreya', 'Raju', 'Gopal', 'Ravi'],
        "Roll": [101, 102, 103, 104, 105],
        "CGPA": [8.15, 8.18, 9.32, 8.85, 7.87]}

# Create a DataFrame object from above dictionary
df = pd.DataFrame(data, index = ['s1', 's2', 's3', 's4', 's5'])
print("This is DataFrame:\n")
print(df)

# Get the index/rows of the above DataFrame 
# Using index attribute
print("\nThis is index of DataFrame:\n")
index_list = df.index
print(index_list)

Output:

This is DataFrame:

      Name  Roll  CGPA
s1  Sanjay   101  8.15
s2  Shreya   102  8.18
s3    Raju   103  9.32
s4   Gopal   104  8.85
s5    Ravi   105  7.87

This is index of DataFrame:

Index(['s1', 's2', 's3', 's4', 's5'], dtype='object')

Method 3: Using index.values property

First, we will create a pandas DataFrame object using the pd.DataFrame() function of pandas Python module. Then we will use the index.values property of pandas DataFrame object to access its index list. As we apply the index.values property on the pandas DataFrame object, it returns an array that represents the data in the index list of the pandas DataFrame object. Let’s get into the Python code to implement this method of getting the DataFrame’s index list.

# Method-3

# Import pandas
import pandas as pd

# Create a Python dictionary
data = {"Name": ['Sanjay', 'Shreya', 'Raju', 'Gopal', 'Ravi'],
        "Roll": [101, 102, 103, 104, 105],
        "Branch": ['ECE', 'CSE', 'EEE', 'ICE', 'IPE'],
        "CGPA": [8.15, 8.18, 9.32, 8.85, 7.87]}

# Create a DataFrame object from above dictionary
df = pd.DataFrame(data)
print("This is DataFrame:\n")
print(df)

# Get the index/rows of the above DataFrame 
# Using index.values property
print("\nThis is index of DataFrame:\n")
index_list = df.index.values
print(index_list)

Output:

This is DataFrame:

     Name  Roll Branch  CGPA
0  Sanjay   101    ECE  8.15
1  Shreya   102    CSE  8.18
2    Raju   103    EEE  9.32
3   Gopal   104    ICE  8.85
4    Ravi   105    IPE  7.87

This is index of DataFrame:

[0 1 2 3 4]

Method 4: Using tolist() function

This is a handy tool of the pandas module which converts the index of a pandas DataFrame object into a Python list. In this method, we create a pandas DataFrame object using the pd.DataFrame() function as we did in the previous methods. Then we will access the pandas DataFrame index object using the index attribute of the pandas DataFrame class. Finally, we will apply the tolist() function which actually returns the index of the DataFrame in the form of a Python list. Let’s write the Python program to implement this handy method to get the index of a pandas DataFrame in a Python list.

# Method-4

# Import pandas
import pandas as pd

# Create a Python dictionary
data = {"Name": ['Sanjay', 'Shreya', 'Raju', 'Gopal', 'Ravi'],
        "Roll": [101, 102, 103, 104, 105],
        "Branch": ['ECE', 'CSE', 'EEE', 'ICE', 'IPE'],
        "CGPA": [8.15, 8.18, 9.32, 8.85, 7.87]}

# Create a DataFrame object from above dictionary
df = pd.DataFrame(data, index = ['R1', 'R2', 'R3', 'R4', 'R5'])
print("This is DataFrame:\n")
print(df)

# Get the index/rows of the above DataFrame 
# Using tolist() function
print("\nThis is index of DataFrame:\n")
index_list = df.index.tolist()
print(index_list)

Output:

This is DataFrame:

      Name  Roll Branch  CGPA
R1  Sanjay   101    ECE  8.15
R2  Shreya   102    CSE  8.18
R3    Raju   103    EEE  9.32
R4   Gopal   104    ICE  8.85
R5    Ravi   105    IPE  7.87

This is index of DataFrame:

['R1', 'R2', 'R3', 'R4', 'R5']

Method 5: Using query() and tolist() functions

Using this method, we can get only the specific indices of the pandas DataFrame object satisfying certain criteria. In this method, we will create a pandas DataFrame object using the pd.DataFrame() function and use the query() function of the pandas DataFrame class. When we apply the query() function on the DataFrame and pass a condition, it returns a DataFrame which contains only the rows which satisfy the condition passed to it.

After this we will apply the index attribute of the DataFrame class and use the tolist() function which returns a Python list of the DataFrame index values.

Let’s see the Python code to implement this useful method to get the selected rows or indices of the pandas DataFrame object satisfying the given conditions.

# Method-5

# Import pandas
import pandas as pd

# Create a Python dictionary
data = {"Name": ['Sanjay', 'Shreya', 'Raju', 'Gopal', 'Ravi'],
        "Roll": [101, 102, 103, 104, 105],
        "Branch": ['ECE', 'CSE', 'EEE', 'ICE', 'IPE'],
        "CGPA": [8.15, 9.32, 8.78, 7.87, 8.85]}

# Create a DataFrame object from above dictionary
df = pd.DataFrame(data, index = ['I', 'II', 'III', 'IV', 'V'])
print("This is DataFrame:\n")
print(df)

# Get the index/rows of the above DataFrame
# Using query() and tolist() functions
print("\nThis is index of DataFrame:\n")
index_list = df.query("CGPA > 8.5").index.tolist()
print(index_list)

Output:

This is DataFrame:

       Name  Roll Branch  CGPA
I    Sanjay   101    ECE  8.15
II   Shreya   102    CSE  9.32
III    Raju   103    EEE  8.78
IV    Gopal   104    ICE  7.87
V      Ravi   105    IPE  8.85

This is index of DataFrame:

['II', 'III', 'V']

Summing-up

In this tutorial, we have learned the four different methods to get the index of the DataFrame object. Hope you have understood the above and are excited to experiment with these methods on your own. Thank you and stay tuned with us for more such kinds of Python tutorials.