Get column index from column name of Pandas DataFrame

Through this Python tutorial, we will go through the different methods on how to Get column index from column name of Pandas Dataframe in Python. And I will show a few examples to get the column indexes of DataFrames in Python.

At the end of this Python tutorial, we will get a clear understanding of indexing the columns of the Pandas DataFrames in Python. Moreover, We will also cover the following topics :

  • Get the column index of the Pandas DataFrame using different methods in Python
    • Get the column index of the Pandas DataFrame using the “DataFrame.columns.get_loc()” function in Python
    • Get the column index of the Pandas DataFrame using the “get_indexer” function in Python
  • Get the column index of the Pandas DataFrame in different formats in Python
    • Get the column indexes in the dictionary format of the Pandas DataFrame in Python
    • Get the indexes of different or multiple columns in a list format of the Pandas DataFrame in Python

Get column index of Pandas DataFrame using different methods in Python

We can get the column indexes of Pandas DataFrame using different methods in Python. But before that, we have to create a DataFrame using Pandas in Python.

Create a Pandas DataFrame in Python

Column Indexes identify every column uniquely. The “index” method in Python helps in identifying every data point in the dataset uniquely. Column Indexes are nothing but column names. Where every column has its own name and has some index that will specify at what location that particular column is located.

Let us create our Pandas DataFrame with multiple rows so that we can extract the indexes of the columns of a DataFrame using the “DataFrame.columns.get_loc(column_name)” function in Python.

Here We have created a dictionary of Loan Applicants’ data that has the names of applicants applying for loans, their credit scores, income, and the Loan status i.e whether the loan is sanctioned or not.

The applicant’s data is later passed to the “pandas.DataFrame” function in order to convert it to a data frame or table i.e in the form of rows and columns.

#Import the necessary libraries
import numpy as np
import pandas as pd

#Create a dictionary which has loan applicants names, their credit score and loan status
data_dict={"Applicant":["Kelvin", "John", "smith", "Robin","Williams","Nick","Anyy","Messi","Jonas","Xavier"],
"Credit_score":[0,1,1,1,0,np.nan,0,0,1,0],
"Income_LPA":[8,6,12,4,8,9,15,21,18,40],
"Loan_status": ["Yes","Yes","No","Yes","Yes","No","Yes","Yes","Yes","No"]}

#Create a DataFrame using Pandas
Applicants_data=pd.DataFrame(data_dict)
Applicants_data

The below image is a table or Pandas DataFrame that describes the applicants’ basic information like the name of the applicant, income, credit score, and loan status i.e whether the applicant is getting a loan or not. Here the credit score of “Nick” is NaN which means not available.

get column indexes of pandas dataframe in python
Create Pandas DataFrame in Python

Here we have created a DataFrame using the Pandas library in Python. Now let us look at different methods to get the column indexes.

Get the column index of Pandas DataFrame using the “DataFrame.columns.get_loc()” function

We can get indexes of the columns too. There is an existing and most widely used function in Python to extract the indexes of the columns of Pandas DataFrame i.e “DataFrame.columns.get_loc(column_name)”.

In which, DataFrame.columns extract all the column names of the dataframe and then “.get_loc” will return the indexes of the columns in Pandas DataFrame in Python.

#Get the Index of the column "Income_LPA" in the Pandas DataFrame
Applicants_data.columns.get_loc("Income_LPA")

Below is the image of the output for the above code, the output “2” indicates that it is the index of the column “Income_LPA” in the Pandas DataFrame that we have created.

get the column indexes of pandas dataframe in python
Get the Index of the column “Income_LPA”

This is how we can get the column index of Pandas DataFrame using the “DataFrame.columns.get_loc()” function in Python.

Get the column index of the Pandas DataFrames using the “get_indexer” function

Here we are trying to get the indexes of different columns using the “get_indexer()” of the Pandas DataFrame in Python.

  • In the below code, we have a columns_index variable that stores the indexes of the columns that we have mentioned in query_columns.
  • The “get_indexer” function in Python fetches the indexes of the columns that are passed inside the function.
# Get the index of columns using get_indexer() in python
query_columns=['Applicant','Loan_status']
columns_index = Applicants_data.columns.get_indexer(query_columns)
print(columns_index)

If we look at the below output image, it is clear that the indexes of the columns that are mentioned in the query_columns(Applicant, Loan_status) are 0 and 3.

get the column indexes of pandas dataframe using get_indexer in python
Get the index of query columns using get_indexer() in Python

This is how we can get the column index of Pandas DataFrame using the “get_indexer()” function in Python.

Get the column index of Pandas DataFrame in different formats

We can even get the column index of Pandas DataFrame in different formats in Python. The two formats that we can extract the column indexes are the list and dictionary formats. Here we will see that 2 ways.

Get the column indexes in the dictionary format of the Pandas DataFrame in Python

Here we are trying to get the column names and their indexes in a dictionary format in which the column names are keys and their corresponding indexes are values in Python.

  • In the below code, we have created an empty dictionary and then we are adding every column and its index to the dictionary by running the “for” loop in Python.
  • Here column names of the pandas dataframe are the keys and for every key, we are assigning the value as its index.
# Displaying  column name with its associated index in a python dictionary
data_dict= {}
for column in Applicants_data.columns:
    data_dict[column] = Applicants_data.columns.get_loc(column)
print(data_dict)

If we look at the below output image, it is clear that the index of the ‘applicant’ column of the pandas dataframe is 0, and Likewise, the index of the column ‘Income_LPA’ of the pandas dataframe that we have created is 2.

get column indexes of pandas dataframe in dictionary format in python
Get the column indexes in the dictionary

This is how can get the column indexes in the dictionary format of the Pandas DataFrame in Python.

Get the indexes of multiple columns in a list format of Pandas DataFrame in Python

Here we are trying to get the indexes of different columns in a list format of the Pandas DataFrame in Python.

  • In the below code, query_columns are the columns that we are trying to get the indexes, and columns_index is a variable storing the indexes of the columns that are mentioned in query_columns
  • The code “for column in query_columns” run the loop to get all the columns that are mentioned in query_columns.
# Get Index for Multiple Column Labels/Names
query_columns=['Applicant','Loan_status']
columns_index=[Applicants_data.columns.get_loc(column) for column in query_columns]
print(columns_index)

If we look at the below output image, it is clear that the indexes of the columns that are mentioned in the query_columns(Applicant, Loan_status) are 0 and 3.

get multiple column indexes of pandas dataframe in list format in python
Get the Indexes for Different Columns of a Pandas DataFrame

This is how we can get the indexes of multiple columns in a list format of Pandas DataFrame in Python.

Conclusion

Through this Python Pandas tutorial, we have covered topics related to getting the indexes of columns of Pandas DataFrame by using methods like “DataFrame.columns.get_loc()”, and “get_indexer” in Python.

Also, We covered how to extract the column indexes of Pandas DataFrames in different formats in Python.

  • Get the column indexes in the dictionary format of the Pandas DataFrame in Python
  • Get the indexes of different or multiple columns in a list format of the Pandas DataFrame in Python

We can also follow the below Python Pandas tutorials for a better understanding of Python.