Get Index of Column in pandas DataFrame in Python (Example)

 

In this tutorial you’ll learn how to find the index of a certain column in a pandas DataFrame in the Python programming language.

The page will consist of the following content:

You’re here for the answer, so let’s get straight to the example…

 

Example Data & Software Libraries

If we want to use the functions of the pandas library, we first need to import pandas:

import pandas as pd                                # Import pandas library in Python

The following data will be used as basement for this Python programming tutorial:

data = pd.DataFrame({'x1':[6, 3, 7, 3, 7, 1, 9],  # Create example DataFrame
                     'x2':['a', 'x', 'y', 's', 's', 'a', 'b'],
                     'x3':[1, 5, 9, 7, 5, 2, 5],
                     'x4':range(8, 1, - 1)})
print(data)                                       # Print example DataFrame

 

table 1 DataFrame get index column pandas dataframe python

 

Table 1 shows that the example data contains seven observations and the four columns “x1”, “x2”, “x3”, and “x4”.

 

Example: Get Index Position of pandas DataFrame Column Using columns Attribute & get_loc() Function

This example explains how to return the index position of a specific column name in a pandas DataFrame using the Python programming language.

For this task, we can use the columns attribute in combination with the get_loc function. Within the get_loc function, we have to specify the variable name for which we want to search as a character string.

Check out the Python syntax below:

print(data.columns.get_loc("x3"))                 # Return column index
# 2

The previous output of the Python console shows the index position of the variable x3 in our pandas DataFrame (i.e. the index position 2). Remember that Python starts the numeration of indices with the index position 0.

 

Video & Further Resources

Do you want to know more about the identification of the index of a certain column in a pandas DataFrame? Then I recommend watching the following video on the YouTube channel of Corey Schafer.

In the video, he demonstrates how to set and reset indices in Python. This also helps to understand the Python code of this article:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition to the video, you could have a look at some of the other articles on this homepage.

 

At this point you should know how to identify the index of a particular column in a pandas DataFrame in Python programming. Don’t hesitate to let me know in the comments section, in case you have any further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top