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

Pandas drop last column: 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? Also, you can find all these methods of a pandas dataframe to remove last column of a dataframe in the form of images for quick reference & easy sharing with friends and others learners.

What is Dataframe?

Delete column python: 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

Drop column in python: 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

Python dataframe delete column: 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.

Use iloc to drop last column of pandas dataframe

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

Python delete 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.

Python drop() function to remove a column

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

Delete column from pandas dataframe: 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.

Python del keyword to delete the column of a dataframe

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.

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

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 to delete the last column of the dataframe

In the above tutorial, we have discussed different ways to delete last column of the dataframe in python. Also, you can learn how to drop last n rows in pandas dataframe by performing import pandas as pd. Thank you & Happy Learnings!