Pandas – Get Column Index For Column Name

You can get the column index from the column name in Pandas using DataFrame.columns.get_loc() method. DataFrame.columns return all column labels of DataFrame as an Index and get_loc() is a method of Index that gives you a column Index for a given column. In this article, I will explain different ways to get an index from column names with examples.

1. Quick Examples of Column Index From Column Name

If you are in a hurry, below are some quick examples of how to get the column index from the column name in pandas DataFrame.


# Get column index from column name i.e column 3.
idx=df.columns.get_loc("Duration")
print("Column Index : "+ str(idx))

# Dictionary of Column name with associated index.
idx_dic = {}
for col in df.columns:
    idx_dic[col] = df.columns.get_loc(col)
print(idx_dic)

# Get Index for Multiple Column Labels/Names
query_cols=['Fee','Courses']
cols_index = [df.columns.get_loc(col) for col in query_cols]
print(cols_index)

# Column index from column name using get_indexer().
cols_index = df.columns.get_indexer(query_cols)

Now, let’s create a Pandas DataFrame with a few duplicate rows on all columns. Our DataFrame contains column names CoursesFeeDuration, and Discount.


# Create DataFrame
import pandas as pd
technologies = {
        'Courses':["Spark","PySpark","Python","pandas"],
        'Fee' :[20000,25000,22000,30000],
        'Duration':['30days','40days','35days','50days'],
        'Discount':[1000,2300,1200,2000]
    
              }
df = pd.DataFrame(technologies)
print(df)

2. Get Column Index From Column Name by get_loc()

DataFrame.columns return all column labels of DataFrame as an Index and Index.get_loc() returns a column Index for a given column.

Syntax of Index.get_loc()


# Syntax for index.get_loc method.
Index.get_loc(key, method=None, tolerance=None)

Example:


# Get column index from column name i.e column 3.
idx=df.columns.get_loc("Duration")
print("Column Index : "+ str(idx))

# Output:
# Column Index : 2

3. Using Dictionary of Column Name With Associated Index.

You can see if we want to create a dictionary with column name as key and associated index as value by idx_dic[] method. For example-


# Dictionary of Column name with associated index.
idx_dic = {}
for col in df.columns:
    idx_dic[col] = df.columns.get_loc(col)
print(idx_dic)

Yields below output.


# Output:
{'Courses': 0, 'Fee': 1, 'Duration': 2, 'Discount': 3}

4. Get Index for Multiple Column Labels/Names

Using the same get_loc() you can get the Index for multiple column labels/names in DataFrame by passing column labels as a list to this method.


# Get Index for Multiple Column Labels/Names
query_cols=['Fee','Courses']
cols_index = [df.columns.get_loc(col) for col in query_cols]
print(cols_index)

# Output:
# cols_index : [1,0]

5. Get Column Index From Column Name Using get_indexer()

In this example, we will see how we will get column index from column name using get_indexer() Method.


# Column index from column name using get_indexer().
cols_index = df.columns.get_indexer(query_cols)

# Output:
# [0,1]

6. Complete Examples –


# Get Column Index From Column Name in Pandas
import pandas as pd
technologies = {
        'Courses':["Spark","PySpark","Python","pandas"],
        'Fee' :[20000,25000,22000,30000],
        'Duration':['30days','40days','35days','50days'],
        'Discount':[1000,2300,1200,2000]
    
              }
df = pd.DataFrame(technologies)
print(df)

# Get column index from column name i.e column 3.
idx=df.columns.get_loc("Duration")
print("Column Index : "+ str(idx))

# Dictionary of Column name with associated index.
idx_dic = {}
for col in df.columns:
    idx_dic[col] = df.columns.get_loc(col)
print(idx_dic)

# Get Index for Multiple Column Labels/Names
query_cols=['Fee','Courses']
cols_index = [df.columns.get_loc(col) for col in query_cols]
print(cols_index)

# Column index from column name using get_indexer().
cols_index = df.columns.get_indexer(query_cols)

Conclusion

In this article, you have learned how to get column Index from a column name by using get_loc(), and get_indexer(). To get the index for multiple column names pass columns as a list to get_loc() method.

Reference

  

Leave a Reply

You are currently viewing Pandas – Get Column Index For Column Name