Pandas Drop Last Column From DataFrame

Spread the love

You can drop the last column from the pandas DataFrame using either iloc[], drop(), pop() functions and del keyword. In this article, I will explain how to drop/delete/remove the last column from Pandas DataFrame with examples.

  • drop() function is used to delete columns or rows from DataFrame;
  • Use axis param to specify what axis you would like to delete. By default axis = 0 meaning to delete rows. Use axis=1 or columns param to delete columns.
  • Use inplace=True to delete row/column in place meaning on existing DataFrame without creating copy.

1. Quick Examples of Drop Last Column From DataFrame

If you are in a hurry, below are some quick examples of how to drop/delete the last column from DataFrame.


# Below are quick example

# Example 1: Drop last column of dataframe 
# using iloc[]
df2 = df.iloc[: , :-1]

# Example 2: By using iloc[] to select all columns
# except the last column
df2 = df[df.columns[:-1]]

# Example 3: Drop last column of dataframe using drop()
df.drop(columns=df.columns[-1],  axis=1,  inplace=True)

# Example 4: Using drop() function to delete last column
df.drop(df.columns[len(df.columns)-1], axis=1, inplace=True)

# Example 5: Using DataFrame.drop() function to drop last column
df2 = df.drop(df.columns[-1],axis=1)

# Example 6: Drop last column of dataframe using pop()
df.pop(df.columns[-1])

# Example 7: Use del keywords
del df[df.columns[-1]] 

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


import pandas as pd
import numpy as np
technologies= ({
    'Courses':["Spark","PySpark","Hadoop","Pandas"],
    'Fee': [22000,25000,30000,35000],
    'Duration':['30days','50days','40days','35days'],
    'Discount':[1000,2000,2500,1500]
              })
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

Yields below output.


# Output
    Courses    Fee Duration  Discount
r1    Spark  22000   30days      1000
r2  PySpark  25000   50days      2000
r3   Hadoop  30000   40days      2500
r4   Pandas  35000   35days      1500

2. Pandas Drop Last Column using iloc[]

Using DataFrame.iloc[] with -1 as index you can drop the last column from Pandas DataFrame. For example use, df.iloc[:,:-1] to select all columns except the last one and then assign it back to the original variable which ideally drops the last column from DataFrame.


# Drop last column of dataframe using iloc[]
df2 = df.iloc[: , :-1]
print(df2)

# By using iloc[] to select all columns
# except the last column
df2 = df[df.columns[:-1]]
print(df2)

Yields below output.


# Output
    Courses    Fee Duration
r1    Spark  22000   30days
r2  PySpark  25000   50days
r3   Hadoop  30000   40days
r4   Pandas  35000   35days

3. Drop Last Column of DataFrame Using drop()

Alternatively, you can also use DataFrame.drop() function to delete the last column. Use axis=1 to specify the columns and inplace=True to apply the change on the existing DataFrame. In the below example, df.columns[-1] returns Duration which is the last column from our DataFrame.


# Drop last column of dataframe using drop()
df.drop(columns=df.columns[-1],  axis=1,  inplace=True)
print(df)

# Using drop() function to delete last column
df.drop(df.columns[len(df.columns)-1], axis=1, inplace=True)
print(df)

# Using DataFrame.drop() function to drop last column
df2 = df.drop(df.columns[-1],axis=1)
print(df2)

Yields the same output as above.

4. Drop Last Column using pop()

Similarly, you can use DataFrame.pop() function to drop the last column of pandas DataFrame and also returned the deleted column as a series. So, you can select the last column of DataFrame by using df.columns[-1], it will select all columns except the last column of DataFrame.


# Drop last column of dataframe using pop()
df.pop(df.columns[-1])
print(df)

Yields the same output as above.

5. Use del Keyword to Drop Last Column of Pandas DataFrame

You can also use del df[df.columns[-1]] to remove the last column of DataFrame.


# Use del keywords
del df[df.columns[-1]] 
print(df)

6. Complete Example For Drop Last Column From DataFrame


import pandas as pd
import numpy as np
technologies= ({
    'Courses':["Spark","PySpark","Hadoop","Pandas"],
    'Fee': [22000,25000,30000,35000],
    'Duration':['30days','50days','40days','35days'],
    'Discount':[1000,2000,2500,1500]
              })
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

# Drop last column of dataframe using iloc[]
df2 = df.iloc[: , :-1]
print(df2)

# By using iloc[] to select all columns
# except the last column
df2 = df[df.columns[:-1]]
print(df2)

# Drop last column of dataframe using drop()
df.drop(columns=df.columns[-1],  axis=1,  inplace=True)
print(df)

# Using drop() function to delete last column
df.drop(df.columns[len(df.columns)-1], axis=1, inplace=True)
print(df)

# Using DataFrame.drop() function to drop last column
df2 = df.drop(df.columns[-1],axis=1)
print(df2)

# Drop last column of dataframe using pop()
df.pop(df.columns[-1])
print(df)

# Use del keywords
del df[df.columns[-1]] 
print(df)

7. Conclusion

In this article, I have explained how to drop the last column from Pandas DataFrame using DataFrame.iloc[], DataFrame.drop(), and DataFrame.pop(), and del Keyword functions with examples.

Happy Learning !!

Related Articles

References

Leave a Reply

You are currently viewing Pandas Drop Last Column From DataFrame