Pandas is an open source library in Python. It provides a variety of tools to work with the tabular data. It revolves around the concept of Dataframes which consists of rows and columns. If you are in a situation where you want to drop a column or group of columns, then Pandas provides you with several methods for this. These methods are listed below:
- drop() Method
- iloc[] Method with drop()
- loc[] Method with drop()
- Iterative Method with del
- pop() Method
Let’s discuss how to drop one or multiple columns in Pandas Dataframes using examples.
Create a Dataframe to perform the operation
In our example, we create a dictionary of lists and then convert it to a Dataframe. Here the key A, B and C of the dictionary defines the column’s name and values define the rows.
import pandas as pd
# Creating a sample dictionary
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9],
'D': [10, 11, 12]
}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# Displaying the DataFrame
print(df)
Output :
Pandas Drop column using df.drop() Method
The drop() method removes the specified column or row from DataFrame. This function takes labels, axis, index, columns, level, inplace., and errors as arguments.
Drop a column by column name
The below code drop the column B from the DataFrame and “axis=1” specifies that the operation is performed along columns. “axis=0” is used for rows. However, it’s important to note that “drop()” method does not modify the original DataFrame unless the “inplace=True” parameter is specified.
# Dropping column 'B'
df = df.drop('B', axis=1)
Drop two or more columns by columns name
you can also remove multiple columns at a time using the drop() method. For this, you need to pass a list containing the names of the columns you want to drop.
# Remove two columns name is 'A' and 'B'
df = df.drop(['A', 'B'], axis=1)
Drop columns by index position
you can use the index position of the columns in place of column names to drop the column using this method. Using below code, we will drop the column ‘B’ using the index position of the column.
# Remove the column 'B' by index
df = df.drop(df.columns[1], axis=1)
Drop column using iloc[] Method
you can use the iloc indexer to select specific columns by their integer location (index) and then use the drop() method to drop a column.
# Dropping column using df.iloc[]
df = df.drop(df.iloc[:, 1:3], inplace=True, axis=1)
Drop column using loc[] Method
The loc[] method allows you to select rows and columns by their labels. So you need to specify the column label you want to drop and then use the drop() method.
# Remove all columns between column name 'B' to 'D'
df= df.drop(df.loc[:, 'B':'D'].columns, axis=1)
Pandas Drop column iteratively
If you want to remove multiple columns iteratively from a DataFrame then it can be done by using the del keyword by looping through a list of column names.
Here we have taken a list containing the names of columns to remove iteratively. The del keyword delete the specified column from the DataFrame by looping through each column name in list.
# List of columns to drop iteratively
columns_to_drop = ['B', 'C']
# Iteratively dropping columns using del
for column in columns_to_drop:
del df[column]
Please note that if you are dealing with important data then use the del keyword cautiously because it directly modifies the DataFrame. Also, once a column is deleted, it cannot be recovered from the DataFrame.
Drop column using pop() Method
You can also remove and return a specific column from a DataFrame using the pop() method in Pandas. To remove the column, you have to specify a column name as an argument in pop() function.
# Dropping a column using pop()
df.pop('B') # Remove and return the column labeled 'B'