In this article we will see how to get column index from column name of a Dataframe. We will use Dataframe.columns attribute and Index.get_loc method of pandas module together.
Syntax: DataFrame.columns
Return: column names index
Syntax: Index.get_loc(key, method=None, tolerance=None)
Return: loc : int if unique index, slice if monotonic index, else mask
Code: Let’s create a Dataframe:
Python3
# import pandas library import pandas as pd # dictionary record = {'Math': [10, 20, 30, 40, 70], 'Science': [40, 50, 60, 90, 50], 'English': [70, 80, 66, 75, 88]} # create a dataframe df = pd.DataFrame(record) # show the dataframe print(df) |
Output:

Dataframe
Example 1: Get a index number of “Science” column.
Python3
# import pandas library import pandas as pd # dictionary record = {'Math': [10, 20, 30, 40, 70], 'Science': [40, 50, 60, 90, 50], 'English': [70, 80, 66, 75, 88]} # give column name col_name = "Science" # find the index no index_no = df.columns.get_loc(col_name) print("Index of {} column in given dataframe is : {}".format(col_name, index_no)) |
Output :

index number of Science column
Example 2: Get a index number of “English” column.
Python3
# import pandas library import pandas as pd # dictionary record = {'Math': [10, 20, 30, 40, 70], 'Science': [40, 50, 60, 90, 50], 'English': [70, 80, 66, 75, 88]} # create a dataframe df = pd.DataFrame(record) # give column name col_name = "English" # find the index no index_no = df.columns.get_loc(col_name) print("Index of {} column in given dataframe is : {}".format(col_name, index_no)) |
Output :

index number of English column
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
- Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc
- Convert a column to row name/index in Pandas
- Create a DataFrame from a Numpy array and specify the index column and column headers
- Get the index of minimum value in DataFrame column
- Get the index of maximum value in DataFrame column
- Return the Index label if some condition is satisfied over a column in Pandas Dataframe
- How to Convert Dataframe column into an index in Python-Pandas?
- How to Convert Index to Column in Pandas Dataframe?
- How to Sort a Pandas DataFrame based on column names or row index?
- How to convert index in a column of the Pandas dataframe?
- Split Pandas Dataframe by Column Index
- Determine Period Index and Column for DataFrame in Pandas
- How to get rows/index names in Pandas dataframe
- Get minimum values in rows or columns with their index position in Pandas-Dataframe
- How to get column names in Pandas dataframe
- Get unique values from a column in Pandas DataFrame
- Get n-smallest values from a particular column in Pandas DataFrame
- Get n-largest values from a particular column in Pandas DataFrame
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

