Built with Tutify.io

    AIStudyLabs

    Pandas Select Columns by Name or Index

    Pandas Select Columns by Name or Index

    In Pandas, selecting columns by name or index allows you to access specific columns in a DataFrame based on their labels (names) or positions (indices). Use loc[] & iloc[] to select a single column or multiple columns from pandas DataFrame by column names/label or index position respectively. Also, refer to a related article how to get cell value from pandas DataFrame.

    In this article, I will explain how to select one or more columns from a DataFrame using different methods such as column labels, index, positions, and ranges.

    Key Points –

    <a id="h-quick-examples-of-select-columns-by-name-or-index"></a>

    Quick Examples of Select Columns by Name or Index

    If you are in a hurry, below are some quick examples of selecting columns by name or index in Pandas DataFrame.

    
    # Quick examples of select columns by name or index
    
    # Example 1: By using df[] notation
    df2 = df[["Courses","Fee","Duration"]] # select multile columns
    
    # Example 2: Using loc[] to take column slices
    df2 = df.loc[:, ["Courses","Fee","Duration"]] # Selecte multiple columns
    df2 = df.loc[:, ["Courses","Fee","Discount"]] # Select Random columns
    df2 = df.loc[:,'Fee':'Discount'] # Select columns between two columns
    df2 = df.loc[:,'Duration':]  # Select columns by range
    df2 = df.loc[:,:'Duration']  # Select columns by range
    df2 = df.loc[:,::2]          # Select every alternate column
    
    # Example 3: Using iloc[] to select column by Index
    df2 = df.iloc[:,[1,3,4]] # Select columns by Index
    df2 = df.iloc[:,1:4] # Select between indexes 1 and 4 (2,3,4)
    df2 = df.iloc[:,2:] # Select From 3rd to end
    df2 = df.iloc[:,:2] # Select First Two Columns
    

    <a id="h-usage-of-pandas-select-columns-by-name-or-index"></a>

    Usage of Pandas Select Columns by Name or Index

    Selecting columns by name or index is a common and essential operation when working with pandas DataFrames.

    First, let's create a pandas DataFrame.

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

    Yields below output.

    <a id="h-using-loc-to-select-columns-by-name"></a>

    Using loc[] to Select Columns by Name

    The df[] and DataFrame.loc[] methods in Pandas provide convenient ways to select multiple columns by names or labels, you can use the syntax [:, start:stop:step] to define the range of columns to include, where the start is the index where the slice starts (inclusive), stop is the index where the slice ends (exclusive), and step is the step size between elements. Another syntax supported by pandas.DataFrame.loc[] is [:, [labels]], where you provide a list of column names as labels.

    
    # loc[] syntax to slice columns
    df.loc[:,start:stop:step]
    

    <a id="h-select-dataframe-columns-by-name"></a>

    Select DataFrame Columns by Name

    To select DataFrame columns by name, you can directly specify the column names within square brackets []. Here, df[['Courses', 'Fee', 'Duration']] select only the Courses, Fee, and Duration columns from the DataFrame df.

    
    # Select Columns by labels
    df2 = df[["Courses","Fee","Duration"]]
    print("Select columns by labels:\n", df2)
    

    Yields below output.

    <a id="h-select-columns-by-index-in-multiple-columns"></a>

    Select Columns by Index in Multiple Columns

    To select multiple columns using df.loc[], you specify both row and column labels. If you want to select all rows and specific columns, you can use : to select all rows and provide a list of column labels. Note that loc[] also supports multiple conditions when selecting rows based on column values.

    
    # Select multiple columns
    df2 = df.loc[:, ["Courses","Fee","Discount"]]
    print("Select multiple columns by labels:\n", df2)
    
    # Output:
    # Select multiple columns by labels:
    #   Courses    Fee  Discount
    # 0    Spark  20000      1000
    # 1  PySpark  25000      2300
    

    In the above example, df.loc[:, ["Courses", "Fee", "Discount"]] selects all rows (:) and the columns labeled Courses, Fee, and Discount from the DataFrame df.

    <a id="h-s-elect-columns-based-on-label-indexing"></a>

    Select Columns Based on Label Indexing

    When you want to select columns based on label Indexes, provide start and stop indexes.

    
    # Select all columns between Fee an Discount columns
    df2 = df.loc[:,'Fee':'Discount']
    print("Select columns by labels:\n", df2)
    
    # Output:
    # Select columns by labels:
    #     Fee Duration  Discount
    # 0  20000   30days      1000
    # 1  25000   40days      2300
    
    # Select from 'Duration' column
    df2 = df.loc[:,'Duration':]
    print("Select columns by labels:\n", df2)
    
    # Output
    # Select columns by labels:
    #  Duration  Discount   Tutor
    # 0   30days      1000  Michel
    # 1   40days      2300     Sam
    
    # Select from beginning and end at 'Duration' column
    df2 = df.loc[:,:'Duration']
    print("Select columns by labels:\n", df2)
    
    # Output
    # Select columns by labels:
    #   Courses    Fee Duration
    # 0    Spark  20000   30days
    # 1  PySpark  25000   40days
    

    <a id="h-select-every-alternate-column"></a>

    Select Every Alternate Column

    You can select every alternate column from a DataFrame, you can use the iloc[] accessor with a step size of 2.

    
    # Select every alternate column
    df2 = df.loc[:,::2]
    print("Select columns by labels:\n", df2)
    
    # Output:
    # Select columns by labels:
    #   Courses Duration   Tutor
    # 0    Spark   30days  Michel
    # 1  PySpark   40days     Sam
    

    This code effectively selects every alternate column, starting from the first column, which results in selecting Courses and Duration.

    <a id="h-pandas-iloc-to-select-column-by-index-or-position"></a>

    Pandas iloc[] to Select Column by Index or Position

    By using pandas.DataFrame.iloc[], to select multiple columns from a DataFrame by their positional indices. You can use the syntax [:, start:stop:step] to define the range of columns to include, where the start is the index where the slice starts (inclusive), stop is the index where the slice ends (exclusive), and step is the step size between elements. Or, you can use the syntax [:, [indices]] with iloc[], where you provide a list of column names as labels.

    <a id="h-select-columns-by-index-position"></a>

    Select Columns by Index Position

    To select multiple columns from a DataFrame by their index positions, you can use the iloc[] accessor. For instance, retrieves Fee, Discount and Duration and returns a new DataFrame with the columns selected.

    
    # Select columns by position
    df2 = df.iloc[:,[1,3,4]]
    print("Selec columns by position:\n", df2)
    
    # Output:
    # Selec columns by position:
    #     Fee  Discount   Tutor
    # 0  20000      1000  Michel
    # 1  25000      2300     Sam
    

    <a id="h-select-columns-by-position-range"></a>

    Select Columns by Position Range

    You can also slice a DataFrame by a range of positions. For instance, select columns by position range using the .iloc[] accessor in Pandas. It selects columns with positions 1 through 3 (exclusive of position 4) from the DataFrame df and assigns them to df2.

    
    # Select between indexes 1 and 4 (2,3,4)
    df2 = df.iloc[:,1:4]
    print("Select columns by position:\n", df2)
    
    # OUtput:
    # Selec columns by position:
    #     Fee Duration  Discount
    # 0  20000   30days      1000
    # 1  25000   40days      2300
    
    # Select From 3rd to end
    df2 = df.iloc[:,2:]
    print("Select columns by position:\n", df2)
    
    # Output:
    # Selec columns by position:
    #  Duration  Discount   Tutor
    # 0   30days      1000  Michel
    # 1   40days      2300     Sam
    
    # Select First Two Columns
    df2 = df.iloc[:,:2]
    print("Selec columns by position:\n", df2))
    
    # Output:
    # Selec columns by position:
    #   Courses    Fee
    # 0    Spark  20000
    # 1  PySpark  25000
    

    To retrieve the last column of a DataFrame, you can use df.iloc[:,-1:], and to obtain just the first column, you can use df.iloc[:,:1].

    <a id="h-complete-example"></a>

    Complete Example

    
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark"],
        'Fee' :[20000,25000],
        'Duration':['30days','40days'],
        'Discount':[1000,2300],
        'Tutor':['Michel','Sam']
                  }
    df = pd.DataFrame(technologies)
    print(df)
    
    # Select multiple columns
    print(df[["Courses","Fee","Duration"]])
    
    # Select Random columns
    print(df.loc[:, ["Courses","Fee","Discount"]])
    
    # Select columns by range
    print(df.loc[:,'Fee':'Discount']) 
    print(df.loc[:,'Duration':])
    print(df.loc[:,:'Duration'])
    
    # Select every alternate column
    print(df.loc[:,::2])
    
    # Selected by column position
    print(df.iloc[:,[1,3,4]])
    
    # Select between indexes 1 and 4 (2,3,4)
    print(df.iloc[:,1:4])
    
    # Select From 3rd to end
    print(df.iloc[:,2:])
    
    # Select First Two Columns
    print(df.iloc[:,:2])
    

    <a id="h-faq-on-pandas-select-columns-by-name-or-index"></a>

    FAQ on Pandas Select Columns by Name or Index

    To select a single column by name, you can use square bracket([]) or dot(.) notation. For example, df['column_name'] or df.column_name

    You can select columns by their index using the df.iloc[] attribute. For example, df.iloc[:, [0, 2]] Use to Select the first and third columns.

    You can use the .loc attribute to select a column by name and .iloc to select by index. For example, df['column_name'] Use to select by name and df.iloc[:, 0] Use to select by index.

    You can select all columns by using a colon : in place of column names or indices. For example, df[:] Use to select all columns.

    You can select columns by their index, but return a DataFrame (not a Series) by using the column names corresponding to those indexes.

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

    Conclusion

    In conclusion, the select columns by name or index in pandas DataFrame using loc[], and iloc[] properties. To understand the similarities and differences between these two refer to pandas loc[] vs iloc[] with examples.

    Happy Learning !!

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

    Related Articles

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

    References