Get column index from column name in python pandas
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When working with pandas DataFrames, you sometimes need the integer position of a column rather than its label. This comes up when interfacing with NumPy arrays, slicing with iloc, or passing column indices to libraries that expect positional indexing. Pandas provides several ways to convert a column name to its positional index. This article covers the main approaches with code examples and explains when to use each one.
Setup
All examples use this sample DataFrame:
Method 1: get_loc()
The get_loc() method on the DataFrame's columns Index object is the most direct and efficient way to find a column's position.
get_loc() uses pandas' internal hash table for lookups, making it O(1) on average. It returns the integer position directly.
For a MultiIndex column structure, get_loc() returns a slice or boolean mask instead of a single integer:
Method 2: list.index()
Convert the columns to a list and use Python's built-in list.index() method.
This is less efficient than get_loc() because it first creates a Python list (O(n) space) and then performs a linear search (O(n) time). For DataFrames with thousands of columns, the difference can be noticeable. For small DataFrames, both methods are effectively instant.
Method 3: Using NumPy's where()
If you need the indices of multiple columns at once, NumPy's where() or argwhere() can be useful.
Method 4: get_indexer() for Multiple Columns
When you need positions for several column names at once, get_indexer() is the cleanest approach.
get_indexer() returns a NumPy array of integer positions. If a column name is not found, the corresponding position is -1:
This makes it easy to check for missing columns without catching exceptions.
Practical Use Cases
Using Column Index with iloc
Reordering Columns by Position
Interfacing with NumPy
Conditional Column Selection
Handling Missing Columns
Both get_loc() and list.index() raise exceptions when the column name does not exist. Wrapping the call in a try-except block is the standard approach.
Alternatively, use get_indexer() which returns -1 for missing columns without raising an exception, as shown earlier.
Performance Comparison
For a DataFrame with 10,000 columns:
For most real-world DataFrames (under a few hundred columns), all methods are fast enough. When column count is large or the lookup is inside a loop, prefer get_loc().
Common Pitfalls
- Calling
get_loc()on the DataFrame itself instead of ondf.columns. The DataFrame does not have aget_locmethod. Usedf.columns.get_loc("name"). - Assuming column indices are stable after adding or dropping columns. Any structural change to the DataFrame can shift column positions.
- Using
list.index()in a tight loop over many column names. Each call converts the columns to a list. Useget_indexer()with a list of names to do the lookup in one call. - Not handling
KeyErrorwhen a column might not exist. This is especially important in data pipelines where upstream schema changes can remove columns. - Confusing
get_loc()(which takes a single label) withget_indexer()(which takes a sequence of labels). Use the right one for your situation.
Summary
Use df.columns.get_loc("col_name") for the fastest single-column lookup. Use df.columns.get_indexer(["col_a", "col_b"]) when you need positions for multiple columns at once. Fall back to df.columns.tolist().index("col_name") only when you need pure Python and do not want to use pandas-specific methods. Always handle the case where a column name might not exist, either with try-except or by checking for -1 in the result of get_indexer().
Related reading
- Get column name based on condition in pandas
- Get list from pandas dataframe column or row?
- Get list of pandas dataframe columns based on data type
- Get minimum Euclidean distance between a given vector and vectors in the database
- Get confidence interval from sklearn linear regression in python
- Get first element from a dictionary
- Get Output From the logging Module in IPython Notebook
- Get pandas.read_csv to read empty values as empty string instead of nan
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.