Built with Tutify.io

    AIStudyLabs

    Pandas Drop First/Last N Columns From DataFrame

    Pandas Drop First/Last N Columns From DataFrame

    In Pandas, you can remove the first/last N columns from a DataFrame using various methods, such as drop() with column names or indices. To remove the first or last n columns from the DataFrame using iloc[], drop(), pop(), or the del keyword functions. In this article, I will explain how to drop the columns of the first and last n from Pandas DataFrame.

    Key Points –

    <a id="h-quick-examples-of-remove-first-last-n-columns"></a>

    Quick Examples of Remove First/Last N Columns

    Below are quick examples of how to drop the first/last n columns.

    
    # Quick examples of drop first/last N columns
    
    # Number of columns to drop
    n = 2
    
    # Using DataFrame.iloc[] 
    # To drop last n columns
    df2 = df.iloc[:, :-n]
    
    # Using drop() function 
    # To delete last n columns
    df.drop(columns=df.columns[-n:], axis=1, inplace=True)
    
    # Using DataFrame.pop() 
    # To drop last n columns
    for i in range(n):
       df.pop(df.columns.values[-1])
    
    # Using del keyword 
    # To drop last n columns
    n = 3
    for i in range(n):
       del df[df.columns.values[-1]]
    
    # Using DataFrame.iloc[] 
    # To drop first n columns
    df2 = df.iloc[:,n:]
    
    # Using drop() function 
    # To delete first n columns
    df.drop(columns=df.columns[:n],axis=1, inplace=True)
    
    # Using DataFrame.pop() 
    # To drop first n columns
    for i in range(n):
       df.pop(df.columns.values[0])
    
    # Using del keyword 
    # To drop first n columns
    for i in range(n):
       del df[df.columns.values[0]]
    

    To run some examples of dropping first/last N columns from Pandas DataFrame, let’s create a Pandas DataFrame.

    
    # Create DataFrame
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas"],
        'Fee' :[20000,25000,22000,24000],
        'Duration':['30days','40days','35days','60days'],
        'Discount':[1000,2300,2500,2000]
                  }
    index_labels=['r1','r2','r3','r4']
    df = pd.DataFrame(technologies,index=index_labels)
    print("Create DataFrame:\n", df)
    

    Yields below output.

    <a id="h-drop-last-n-columns-from-pandas-dataframe"></a>

    Drop Last N Columns From Pandas DataFrame

    To drop the last n columns from the pandas DataFrame using either iloc[], drop(), del keyword, and pop() methods.

    <a id="h-using-iloc"></a>

    Using iloc[]

    You can use DataFrame.iloc[] the indexing syntax [:,:-n] with n as an integer to select the last n columns from pandas DataFrame. Let’s drop the last two columns of df and store the resulting DataFrame as a separate pandas DataFrame.

    
    # Using DataFrame.iloc[] 
    # To drop last n columns
    n = 2
    df2 = df.iloc[:,:-n]
    print("After dropping last n columns:\n", df2)
    

    Output.

    <a id="h-using-drop"></a>

    Using drop()

    You can also use DataFrame.drop() method to delete the last n columns. Use axis=1 to specify the columns and inplace=True to apply the change on the existing DataFrame. On below example df.columns[-n:] returns the last n column labels that would be deleting.

    
    # Using drop() function 
    # To delete last n columns
    n = 2
    df.drop(columns=df.columns[-n:], axis=1, inplace=True)
    print(df)
    
    # Output:
    #     Courses    Fee
    # r1    Spark  20000
    # r2  PySpark  25000
    # r3   Python  22000
    # r4   pandas  24000
    

    <a id="h-using-pop"></a>

    Using pop()

    Alternatively, You can also use for i in range(n): df.pop(df.columns.values[-1]) to remove the last n columns from Pandas DataFrame.

    
    # Using dataframe.pop()
    # To drop last n columns
    n = 2
    for i in range(n):
            df.pop(df.columns.values[-1])
    print(df)
    
    # Output:
    #     Courses    Fee
    # r1    Spark  20000
    # r2  PySpark  25000
    # r3   Python  22000
    # r4   pandas  24000
    

    <a id="h-using-del-keyword-to-drop-last-n-columns-of-dataframe"></a>

    Using del keyword to Drop Last N Columns of DataFrame

    By iterating over the last n column names of pandas DataFrame and for each of them select the column by passing column name in subscript operator to remove last N columns.

    
    # Using del keyword 
    # To drop last n columns
    n = 3
    for i in range(n):
            del df[df.columns.values[-1]]
    print(df)
    
    # Output:
    #     Courses
    # r1    Spark
    # r2  PySpark
    # r3   Python
    # r4   pandas
    

    <a id="h-delete-first-n-columns-of-pandas-dataframe"></a>

    Delete First N Columns of Pandas DataFrame

    Use iloc[], drop(), pop(), and del keyword methods to drop the top/first n columns from the pandas DataFrame.

    <a id="h-using-iloc-0"></a>

    Using iloc[]

    Use the DataFrame's iloc[] indexing syntax [:, n:], where n is an integer, to select all columns except the first n columns from a Pandas DataFrame. For instance, df.iloc[:, n:] will exclude the first n columns, with n being the number of columns you want to remove.

    
    # Using DataFrame.iloc[] 
    # To drop first n columns
    n = 2
    df2 = df.iloc[:,n:]
    print(df2)
    
    # Output:
    #     Courses    Fee
    # r1    Spark  20000
    # r2  PySpark  25000
    # r3   Python  22000
    # r4   pandas  24000
    

    <a id="h-using-drop-0"></a>

    Using drop()

    You can also use DataFrame.drop() method to remove the first n columns. Use columns param to specify the columns and inplace=True to apply the change on the existing DataFrame. In the below example df.columns[:n] return the first n columns.

    
    # Using drop() function 
    # To delete first n columns
    n = 2
    df.drop(columns=df.columns[:n],axis=1, inplace=True)
    print(df)
    
    # Output:
    #     Courses    Fee
    # r1    Spark  20000
    # r2  PySpark  25000
    # r3   Python  22000
    # r4   pandas  24000
    

    <a id="h-using-pop-0"></a>

    Using pop()

    Alternatively, You can also use for i in range(n): df.pop(df.columns.values[0]) to delete the first n columns from pandas DataFrame.

    
    # Using DataFrame.pop() 
    # To drop first n columns
    n = 2
    for i in range(n):
            df.pop(df.columns.values[0])
    print(df)
    
    # Output:
    #     Courses    Fee
    # r1    Spark  20000
    # r2  PySpark  25000
    # r3   Python  22000
    # r4   pandas  24000
    

    <a id="h-using-the-del-keyword"></a>

    Using the del keyword

    By using the iterate over the first n column names of pandas DataFrame and for each of them select the column by passing column name in subscript operator to remove first n columns.

    
    # Using del keyword 
    # To drop first n columns
    n = 3
    for i in range(n):
            del df[df.columns.values[0]]
    print(df)
    
    # Output:
    #     Discount
    # r1      1000
    # r2      2300
    # r3      2500
    # r4      2000
    

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

    Complete Example

    
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas"],
        'Fee' :[20000,25000,22000,24000],
        'Duration':['30days','40days','35days','60days'],
        'Discount':[1000,2300,2500,2000]
                  }
    index_labels=['r1','r2','r3','r4']
    df = pd.DataFrame(technologies,index=index_labels)
    print(df)
    
    # Using DataFrame.iloc[] 
    # To drop last n columns
    n = 2
    df2 = df.iloc[: , :-n]
    print(df2)
    
    # Using drop() function 
    # To delete last n columns
    n = 2
    df.drop(columns=df.columns[-n:], 
            axis=1, 
            inplace=True)
    print(df)
    
    # Using DataFrame.pop() 
    # To drop last n columns
    n = 2
    for i in range(n):
            df.pop(df.columns.values[-1])
    print(df)
    
    # Using del keyword 
    # To drop last n columns
    n = 3
    for i in range(n):
            del df[df.columns.values[-1]]
    print(df)
    
    # Using DataFrame.iloc[] 
    # To drop first n columns
    n = 2
    df2 = df.iloc[:,n:]
    print(df2)
    
    # Using drop() function 
    # To delete first n columns
    n = 2
    df.drop(columns=df.columns[:n], 
            axis=1, 
            inplace=True)
    print(df)
    
    # Using DataFrame.pop() 
    # To drop first n columns
    n = 2
    for i in range(n):
            df.pop(df.columns.values[0])
    print(df)
    
    # Using del keyword 
    # To drop first n columns
    n = 3
    for i in range(n):
            del df[df.columns.values[0]]
    print(df)
    

    <a id="h-faqs-on-delete-first-last-n-columns-from-dataframe"></a>

    FAQs on Delete First/Last N Columns From DataFrame

    To delete the last N columns from a pandas DataFrame, you can use the iloc method with negative indexing. This code selects all rows (:) and all columns up to the last N columns (:-n). The negative indexing -n specifies to exclude the last N columns.

    To delete the first N columns from a pandas DataFrame, you can use the iloc method. This code selects all rows (:) and columns starting from the Nth column to the end. The iloc[:, n:] part effectively excludes the first N columns.

    You can drop both the first and last N columns from a Pandas DataFrame. To do this, you can combine the methods for dropping the first N columns and the last N columns.

    You can delete specific columns from a pandas DataFrame by name using the DataFrame.drop() method. This code will remove the specified columns ('Column1', 'Column2', and 'Column3') from your DataFrame. The drop() method allows you to delete columns based on their names rather than their positions.

    If N is larger than the total number of columns in a DataFrame and you attempt to delete the first or last N columns.

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

    Conclusion

    In this article, I have explained how to remove the first and last n columns from DataFrame using various methods such as iloc[], drop(), pop(), and del keyword function with examples.

    Happy Learning !!

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

    Related Articles

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

    References