Built with Tutify.io

    AIStudyLabs

    Pandas Get Column Name by Index or Position

    Pandas Get Column Name by Index or Position

    In Pandas, you can get the column name by index or position using the columns attribute or the iloc[] indexer. Sometimes you may have a column index and want to get column name by index in pandas DataFrmae, you can do so by using DataFrame.columns[idx]. Note that the index starts from 0.

    Key Points –

    <a id="h-1-quick-examples-of-get-column-name-by-index-or-position"></a>

    1. Quick Examples of Get Column Name by Index or Position

    If you are in a hurry, below are some quick examples of how to get column name by index or position

    
    # Quick examples of get column name by index
    
    # Example 1: Get column name by index 
    print(df.columns)
    
    # Example 2: Get column name by index
    index_to_get = 2
    df2 = df.columns[index_to_get]
    
    # Example 3: Get column name by index 
    # Using the columns attribute
    df2 = df.columns[2]
    
    # Example 4: Get multiple column names by index
    indices_to_get = [1, 3]  
    df2 = df.columns[indices_to_get]
    
    # Example 5: Get column name by index 
    # Using iloc[]
    index_to_get = 2
    df2 = df.columns[df.iloc[:, index_to_get].name]
    

    <a id="h-2-get-column-name-by-index-example"></a>

    2. Get Column Name by Index Example

    DataFrame.columns returns an object of pandas.core.indexes.base.Index and use the position by using [] operator to get the column name by index or position.

    Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are Courses, Fee, Duration and Discount

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

    Yields below output.

    Now let's get the column names from pandas DataFrame, As I said the below example returns an Index object containing all column names.

    
    # Get column name by index 
    print(df.columns)
    
    # Output:
    # Index(['Courses', 'Fee', 'Duration', 'Discount'],
    # dtype='object')
    

    <a id="h-3-using-columns-attribute"></a>

    3. Using Columns Attribute

    You can use the columns attribute directly to get the column name by index. For instance, the df.columns returns an Index object containing the column names, and you can access a specific column name by its index using standard list indexing.

    
    # Get column name by index
    index_to_get = 2
    df2 = df.columns[index_to_get]
    print(f"Column name at index {index_to_get}: {df2}")
    
    # Get column name by index 
    # Using the columns attribute
    df2 = df.columns[2]
    print(f"Column name at index {2}: {df2}")
    
    # Output:
    # Column name at index 2: Duration
    

    <a id="h-4-get-multiple-column-names-by-index"></a>

    4. Get Multiple Column Names by Index

    Similarly, you can get multiple column names by index in Pandas by providing a list of indices. For instance, df.columns[indices_to_get] returns a new Index object containing the column names corresponding to the specified list of indices. Adjust the df2 list based on the indices you want to retrieve.

    
    # Get multiple column names by index
    indices_to_get = [1, 3]  # List of indices
    df2 = df.columns[indices_to_get]
    print(f"Column names at indices {indices_to_get}: {df2}")
    
    # Output:
    # Column names at indices [1, 3]: Index(['Fee', 'Discount'], dtype='object')
    

    <a id="h-5-using-the-iloc-indexer"></a>

    5. Using the iloc[] Indexer

    You can use the iloc[] indexer to get the column name by index. For example, df.iloc[:, index_to_get].name gives you the column name corresponding to the specified index. The df.iloc[:, index_to_get] part selects the column as a Pandas Series and .name retrieves the column name.

    
    # Get column name by index using iloc[]
    index_to_get = 2
    df2 = df.columns[df.iloc[:, index_to_get].name]
    print(f"Column name at index {index_to_get}: {df2}")
    
    # Output:
    # Column name at index 2: Duration
    

    <a id="h-frequently-asked-questions-on-get-column-name-by-index-or-position"></a>

    Frequently Asked Questions on Get Column Name by Index or Position

    To get the column name by index in Pandas, you can use the columns attribute of the DataFrame. For example, df.columns returns an Index object containing the column names, and you can access the column name by providing the desired index within square brackets. Remember that column indices in Pandas are zero-based, so the first column has an index of 0, the second column has an index of 1, and so on. Adjust the index_to_get variable accordingly based on your specific DataFrame and requirements.

    The iloc indexer is primarily used for integer-location-based indexing on the rows and columns of a DataFrame. While it can be used to get the column name indirectly, it's not the most straightforward approach.

    You can get multiple column names by index in Pandas by providing a list of indices. For example, df.columns[indices_to_get] returns a new Index object containing the column names corresponding to the specified list of indices. Adjust the indices_to_get list based on the indices you want to retrieve.

    In Pandas, the column index is zero-based. This means that the first column in a DataFrame has an index of 0, the second column has an index of 1, and so on. When working with DataFrame columns in Pandas, it's important to keep in mind this zero-based indexing convention.

    You can use negative indices to access columns from the end in Pandas. Negative indices count from the end of the column index. For example, -1 refers to the last column, -2 refers to the second-to-last column, and so on.

    If you provide an out-of-range index when trying to access a column in Pandas, you will get an IndexError. The error will indicate that the index is out of bounds.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained you can retrieve the column name by index in a Pandas DataFrame using either the columns attribute or the iloc[] indexer.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles