How to Get an Index of Pandas DataFrame?

DataFrames are two-dimensional tabular data structures in Pandas that are size-mutable and heterogeneous. They have labeled axes (rows and columns). The DataFrame is one of the Pandas’ most widely used data structures.

Using different Pandas functions, we can perform various operations on a DataFrame, such as data slicing, reshaping, filtering, etc. To get the index of pandas DataFrame, various methods are used in Python, such as “df.index”, “df.index.values”, etc.

In this Python blog, the below-provided methods will be explained to get the index of pandas DataFrame using numerous examples:

Method 1: Using the “.index” Attribute of Pandas DataFrame

In the example code given below, the “df.index” attribute of pandas DataFrame is used to return the index of input DataFrame:

Code:

import pandas as pd
df = pd.DataFrame({'Team-1': ['Henry','Joseph','Lily'],
                   'Team-2': ['Alexa','Anna','John']},
                    index=['1', '2', '3'])
print('DataFrame: \n',df)
print('\nIndex of DataFrame: \n',df.index)

In the above code:

  • The “pandas” DataFrame function “pd.DataFrame()” is used to create the DataFrame.
  • The user-specified index value is passed as an index parameter named “index”.
  • The pandas’ DataFrame index attribute is used to get the index of the given DataFrame. The DataFrame’s “df.index” attribute returns a tuple containing its DataFrame index as a list.

Output:

The above output shows the created DataFrame and the index of the DataFrame.

Method 2: Using the “tolist()” Function

In the below code, the “tolist()” function is used to get the index of the pandas DataFrame by converting the index object into the list:

Code:

import pandas as pd
df = pd.DataFrame({'Team-1': ['Henry','Joseph','Lily'],
                   'Team-2': ['Alexa','Anna','John']},
                    index=['a', 'b', 'c'])
print('DataFrame: \n',df)
print('\nIndex of DataFrame: \n',df.index.tolist())

In the above code:

  • The panda’s DataFrame function “pd.DataFrame” is used to create the DataFrame having user-defined index values “a,b,c”.
  • The “df.index.values.tolist()” function retrieves the index of the given DataFrame as a list.

Output:

Based on the above snippet, the index of DataFrame was successfully obtained using the “tolist()” function.

Method 3: Using the “index.values”

In the below code, the “index.values” property is used to get the index of the given DataFrame:

Code:

import pandas as pd
df = pd.DataFrame({'Team-1': ['Henry','Joseph','Lily'],
                   'Team-2': ['Alexa','Anna','John']},
                    index=['x', 'y', 'z'])
print('DataFrame: \n',df)
print('\nIndex of DataFrame: \n',df.index.values)

In the above code, the “df.index.values” function is used to get the index values of the input DataFrame. This function returns an array of index lists of DataFrame.

Output:

In the above output, we successfully find the index of DataFrame using the “index.values” property.

Method 4: Using the “.axes” attribute

In the below example, the “.axes” attribute of pandas DataFrame is used to get the index of the given DataFrame.

Code:

import pandas as pd
df = pd.DataFrame({'Team-1': ['Henry','Joseph'],
                   'Team-2': ['Alexa','Anna']},
                    index=['0', '1'])
print('DataFrame: \n',df)
print('\nIndex of DataFrame: \n',df.axes[0])

In the above code:

  • The “df.axes[]” is utilized to get the index of the given DataFrame.
  • This “df.axes[]” attribute will return the list containing the row and column labels of DataFrame.
  • The “index=0” stored the row labels, and the “index=1” stored the column label.

Output:

The above output shows the index of DataFrame using the “df.axes[0]” function.

Conclusion

To get the index of pandas DataFrame, the “df.index” attribute, “tolist()” function, “index.values”, and “.axes” attributes are used in Python. The “df.index” is used to get the index of DataFrame by returning the list of tuples. Similarly, the “tolist()” and “index.values” are also used to get the index of Pandas DataFrame. This article presented a detailed guide on how to find the index of Pandas DataFrame utilizing numerous examples.