Pandas: Delete last column of dataframe in python | How to Remove last column from Dataframe in Python?

In this article, we will discuss different ways to delete the last column of a pandas dataframe in python or how to drop the last columns in Pandas Dataframe. Along with that, you may also study what is Dataframe and How to Remove a Column From a Python Dataframe?

What is Dataframe?

A Data structure provided by the Python Pandas module is known as a DataFrame. It stores values in the form of rows and columns. So, we can have the data in the form of a matrix outlining the entities as rows and columns. A DataFrame relates an Excel or CSV file to the real world.

Different Methods to Drop last columns in Pandas Dataframe

Here are few methods that are used to remove the last columns of DataFrame in Python.

  1. Use iloc()
  2. Use drop()
  3. Use del()
  4. Use pop()

Use iloc to drop last column of pandas dataframe

In this method, we are going to use iloc to drop the last column of the pandas dataframe. In python, pandas have an attribute iloc to select the specific column using indexing.

Syntax:

df.iloc[row_start:row_end , col_start, col_end]

So we will be using the above syntax which will give rows from row_star to row_end and columns from col_start to col_end1.

import pandas as pd
employees = [('Abhishek', 34, 'Sydney') ,
             ('Sumit', 31, 'Delhi') ,
             ('Sampad', 16, 'New York') ,
             ('Shikha', 32,'Delhi') , ]
#load data into a DataFrame object:
df = pd.DataFrame(employees, columns=['Name', 'Age', 'City'], index=['a', 'b', 'c', 'd'])
print("Contents of the Dataframe : ")
print(df)
# Drop last column of a dataframe
df = df.iloc[: , :-1]
print("Modified Dataframe : ")
print(df)

Output:

Contents of the Dataframe :
        Name       Age   City
a     Abhishek   34     Sydney
b     Sumit        31     Delhi
c     Sampad     16     New York
d    Shikha        32     Delhi

Modified Dataframe :
        Name     Age
a     Abhishek 34
b     Sumit      31
c     Sampad  16
d     Shikha    32

So in the above example, we have to remove last column from dataframe which is ‘city’, so we just selected the column from position 0 till one before the last one…means from column 0 to -2 we selected 0 to -1, this deleted last column.

Must Check:

Python drop() function to remove a column

In this, we are going to use drop() to remove the last column of the pandas dataframe.

This function accepts a sequence of column names that need to delete from the dataframe. Here we ‘axis=1’ for delete column only and ‘inplace=True’ we use to pass arguments.

import pandas as pd
employees = [('Abhishek', 34, 'Sydney') ,
             ('Sumit', 31, 'Delhi') ,
             ('Sampad', 16, 'New York') ,
             ('Shikha', 32,'Delhi') , ]
#load data into a DataFrame object:
df = pd.DataFrame(employees, columns=['Name', 'Age', 'City'], index=['a', 'b', 'c', 'd'])
print("Contents of the Dataframe : ")
print(df)
# Drop last column
df.drop(columns=df.columns[-1], axis=1, inplace=True)
print("Modified Dataframe : ")
print(df)

Output:

Contents of the Dataframe :

       Name        Age   City
a     Abhishek   34     Sydney
b     Sumit        31     Delhi
c     Sampad     16     New York
d    Shikha        32     Delhi

Modified Dataframe :

       Name      Age
a     Abhishek   34
b     Sumit        31
c     Sampad    16
d     Shikha      32

Python del keyword to remove the column

Here we are going to use a del() keyword to drop last column of pandas dataframe.

So in this method, we are going to use del df[df.columns[-1]]for deleting the last column. We will use -1 because we are selecting from last.

import pandas as pd
employees = [('Abhishek', 34, 'Sydney') ,
             ('Sumit', 31, 'Delhi') ,
             ('Sampad', 16, 'New York') ,
             ('Shikha', 32,'Delhi') , ]
#load data into a DataFrame object:
df = pd.DataFrame(employees, columns=['Name', 'Age', 'City'], index=['a', 'b', 'c', 'd'])
print("Contents of the Dataframe : ")
print(df)
# Delete last column
del df[df.columns[-1]]
print("Modified Dataframe : ")
print(df)

Output:

Contents of the Dataframe :
       Name     Age   City
a     Abhishek 34    Sydney
b      Sumit     31    Delhi
c     Sampad   16    New York
d     Shikha     32    Delhi
Modified Dataframe :
     Name      Age
a Abhishek    34
b Sumit          31
c Sampad      16
d Shikha        32

Drop last columns of Pandas Dataframe Using Python dataframe.pop() method

In this, we are going to use the pop(column_name) method to drop last column of pandas dataframe. It expects a column name as an argument and deletes that column from the calling dataframe object.

import pandas as pd
employees = [('Abhishek', 34, 'Sydney') ,
             ('Sumit', 31, 'Delhi') ,
             ('Sampad', 16, 'New York') ,
             ('Shikha', 32,'Delhi') , ]
#load data into a DataFrame object:
df = pd.DataFrame(employees, columns=['Name', 'Age', 'City'], index=['a', 'b', 'c', 'd'])
print("Contents of the Dataframe : ")
print(df)
df.pop(df.columns[-1])
print("Modified Dataframe : ")
print(df)

Output:

Contents of the Dataframe :

        Name    Age    City
a    Abhishek  34   Sydney
b    Sumit       31   Delhi
c    Sampad   16    New York
d   Shikha      32    Delhi

Modified Dataframe :

     Name      Age
a   Abhishek 34
b   Sumit      31
c   Sampad  16
d  Shikha     32

Conclusion:

In the above tutorial, we have discussed different ways to delete last column of the dataframe in python. Thank you & Happy Learnings!