Get column index from column name of a given Pandas DataFrame Last Updated : 15 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Let's learn how to get the index of a column based on its name using the DataFrame.columns attribute and Index.get_loc() method from the pandas library.Index.get_loc() Method Index.get_loc() function finds the index of a specified column name and returns an integer if the column name is unique.Syntax: Index.get_loc(key, method=None, tolerance=None)Let’s create a DataFrame with some sample data and find the index of specific columns by name. Python import pandas as pd # Sample data data = { "Math": [90, 85, 78], "Science": [88, 92, 95], "English": [85, 80, 89] } # Creating the DataFrame df = pd.DataFrame(data) print("DataFrame:") print(df) Output: Example 1: Get the Index of the "Science" Column Python # Get index of the "Science" column science_index = df.columns.get_loc("Science") print("Index of 'Science' column:", science_index) Output: Index of 'Science' column: 1get_loc method is the most recommended method. There are several other methods than can be implemented to get column index from column name of a given Pandas DataFrame. Using list().index()Convert the DataFrame columns to a list and use .index() method to get index of the column. Python column_index = list(df.columns).index('Science') print("Using list().index():", column_index) Output: Using list().index(): 1Similar to using list() here, we can also use .tolist(), like this - column_index = df.columns.tolist().index('Math'). If the performance is critical, .tolist() can be slightly slower. Using np.where() with columns If you have many columns and need a fast method, you can use numpy.where(). Python import numpy as np column_index = np.where(df.columns == 'Science')[0][0] print("index of science column using np.where():", column_index) Output: index of science column using np.where(): 1 Comment More infoAdvertise with us Next Article Get column index from column name of a given Pandas DataFrame A ankthon Follow Improve Article Tags : Python Python-pandas Python pandas-dataFrame Practice Tags : python Similar Reads Get list of column headers from a Pandas DataFrame In this article, we will see, how to get all the column headers of a Pandas DataFrame as a list in Python. The DataFrame.column.values attribute will return an array of column headers. pandas DataFrame column namesUsing list() Get Column Names as List in Pandas DataFrame In this method we are using 3 min read How to convert index in a column of the Pandas dataframe? Each row in a dataframe (i.e level=0) has an index value i.e value from 0 to n-1 index location and there are many ways to convert these index values into a column in a pandas dataframe. First, let's create a Pandas dataframe. Here, we will create a Pandas dataframe regarding student's marks in a pa 4 min read Change column names and row indexes in Pandas DataFrame Given a Pandas DataFrame, let's see how to change its column names and row indexes. About Pandas DataFramePandas DataFrame are rectangular grids which are used to store data. It is easy to visualize and work with data when stored in dataFrame. It consists of rows and columns.Each row is a measuremen 4 min read Capitalize first letter of a column in Pandas dataframe Analyzing real-world data is somewhat difficult because we need to take various things into consideration. Apart from getting the useful data from large datasets, keeping data in required format is also very important. One might encounter a situation where we need to capitalize any specific column i 2 min read How to Convert Dataframe column into an index in Python-Pandas? Pandas provide a convenient way to handle data and its transformation. Let's see how can we convert a data frame column to row name or index in Pandas. Create a dataframe first with dict of lists.  Python3 # importing pandas as pd import pandas as pd # Creating a dict of lists data = {'Name':["Akas 2 min read Like