Open In App

How to drop one or multiple columns in Pandas Dataframe

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Let’s discuss how to drop one or multiple columns in Pandas Dataframe. To Delete a column from a Pandas DataFrame or Drop one or multiple columns in a Pandas Dataframe can be achieved in multiple ways. 

Drop One or Multiple Columns in Pandas Dataframe

There are various methods to drop one or multiple columns in Pandas Dataframe, we are discussing some generally used methods for dropping one or multiple columns in Pandas Dataframe which are the following :

Create a DataFrame

First we creates a simple Dataframe with dictionary of lists, say column names are A, B, C, D, E. In this article, we will cover 6 different methods to delete some columns from Pandas DataFrame.

Python3




# Import pandas package
import pandas as pd
 
# create a dictionary with five fields each
data = {
    'A': ['A1', 'A2', 'A3', 'A4', 'A5'],
    'B': ['B1', 'B2', 'B3', 'B4', 'B5'],
    'C': ['C1', 'C2', 'C3', 'C4', 'C5'],
    'D': ['D1', 'D2', 'D3', 'D4', 'D5'],
    'E': ['E1', 'E2', 'E3', 'E4', 'E5']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
df


Output :

    A   B   C   D   E
0  A1  B1  C1  D1  E1
1  A2  B2  C2  D2  E2
2  A3  B3  C3  D3  E3
3  A4  B4  C4  D4  E4
4  A5  B5  C5  D5  E5

Dataframe Drop Column in Pandas using df.drop() Method

Example 1: In this example we Remove specific single columns as the below code uses Pandas to create a DataFrame from a dictionary and then removes the column ‘A’ using the drop method with axis=1. However, it’s important to note that the original DataFrame (‘df’) remains unchanged unless the inplace=True parameter is used or the result is assigned back to ‘df’.

Python3




# Import pandas package
import pandas as pd
 
# create a dictionary with five fields each
data = {
    'A': ['A1', 'A2', 'A3', 'A4', 'A5'],
    'B': ['B1', 'B2', 'B3', 'B4', 'B5'],
    'C': ['C1', 'C2', 'C3', 'C4', 'C5'],
    'D': ['D1', 'D2', 'D3', 'D4', 'D5'],
    'E': ['E1', 'E2', 'E3', 'E4', 'E5']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Remove column name 'A'
df.drop(['A'], axis=1)


Output :

    A   B   C   D   E
0  A1  B1  C1  D1  E1
1  A2  B2  C2  D2  E2
2  A3  B3  C3  D3  E3
3  A4  B4  C4  D4  E4
4  A5  B5  C5  D5  E5

Example 2: In this example Remove specific multiple columns as the below code uses Pandas to create a DataFrame from a dictionary and then removes columns ‘C’ and ‘D’ using the drop method with axis=1. However, note that the original DataFrame (‘df’) remains unchanged unless the result is assigned back or inplace=True is used. Alternatively, the same operation can be performed using df.drop(columns=['C', 'D']).

Python3




# Import pandas package
import pandas as pd
 
# create a dictionary with five fields each
data = {
    'A': ['A1', 'A2', 'A3', 'A4', 'A5'],
    'B': ['B1', 'B2', 'B3', 'B4', 'B5'],
    'C': ['C1', 'C2', 'C3', 'C4', 'C5'],
    'D': ['D1', 'D2', 'D3', 'D4', 'D5'],
    'E': ['E1', 'E2', 'E3', 'E4', 'E5']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Remove two columns name is 'C' and 'D'
df.drop(['C', 'D'], axis=1)
 
# df.drop(columns =['C', 'D'])


Output :

    A   B   E
0  A1  B1  E1
1  A2  B2  E2
2  A3  B3  E3
3  A4  B4  E4
4  A5  B5  E5

Example 3: In this example Remove columns based on column index as the below code creates a Pandas DataFrame from a dictionary and removes three columns (‘A’, ‘E’, ‘C’) based on their index positions using the `drop` method with `axis=1`. The modified DataFrame is displayed, and the changes are made in place (`inplace=True`).

Python3




# Import pandas package
import pandas as pd
 
# create a dictionary with five fields each
data = {
    'A': ['A1', 'A2', 'A3', 'A4', 'A5'],
    'B': ['B1', 'B2', 'B3', 'B4', 'B5'],
    'C': ['C1', 'C2', 'C3', 'C4', 'C5'],
    'D': ['D1', 'D2', 'D3', 'D4', 'D5'],
    'E': ['E1', 'E2', 'E3', 'E4', 'E5']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Remove three columns as index base
df.drop(df.columns[[0, 4, 2]], axis=1, inplace=True)
 
df


Output :

    B   D
0  B1  D1
1  B2  D2
2  B3  D3
3  B4  D4
4  B5  D5

Dataframe Drop Columns in Pandas using df.iloc[] Method

In this example Delete columns between specific start and end columns as the below code utilizes Pandas to create a DataFrame from a dictionary and then removes all columns between column indices 1 to 3 using the drop method with axis=1. The changes are made in place (inplace=True), and the modified DataFrame is displayed.

Python3




# Import pandas package
import pandas as pd
# create a dictionary with five fields each
data = {
    'A': ['A1', 'A2', 'A3', 'A4', 'A5'],
    'B': ['B1', 'B2', 'B3', 'B4', 'B5'],
    'C': ['C1', 'C2', 'C3', 'C4', 'C5'],
    'D': ['D1', 'D2', 'D3', 'D4', 'D5'],
    'E': ['E1', 'E2', 'E3', 'E4', 'E5']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Remove all columns between column index 1 to 3
df.drop(df.iloc[:, 1:3], inplace=True, axis=1)
 
df


Output :

    A   D   E
0  A1  D1  E1
1  A2  D2  E2
2  A3  D3  E3
3  A4  D4  E4
4  A5  D5  E5

Pandas Drop Columns from Dataframe using df.ix() method. 

In this example Remove columns between specific column names as the below code, using Pandas, creates a DataFrame from a dictionary and removes all columns between the column names ‘B’ to ‘D’ using the drop method with axis=1. However, the original DataFrame (‘df’) remains unchanged unless the result is assigned back or inplace=True is used.

Python3




# Import pandas package
import pandas as pd
 
# create a dictionary with five fields each
data = {
    'A': ['A1', 'A2', 'A3', 'A4', 'A5'],
    'B': ['B1', 'B2', 'B3', 'B4', 'B5'],
    'C': ['C1', 'C2', 'C3', 'C4', 'C5'],
    'D': ['D1', 'D2', 'D3', 'D4', 'D5'],
    'E': ['E1', 'E2', 'E3', 'E4', 'E5']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Remove all columns between column name 'B' to 'D'
df.drop(df.ix[:, 'B':'D'].columns, axis=1)


Output :

    A   E
0  A1  E1
1  A2  E2
2  A3  E3
3  A4  E4
4  A5  E5

Pandas Drop Columns from Dataframe using df.loc[] Method

In this example Drop columns between specific column names as below code uses Pandas to create a DataFrame from a dictionary and then removes all columns between column names ‘B’ and ‘D’ using the drop method with axis=1. The modified DataFrame is not assigned back to any variable, and the original DataFrame remains unchanged.

Python3




# Import pandas package
import pandas as pd
 
# create a dictionary with five fields each
data = {
    'A': ['A1', 'A2', 'A3', 'A4', 'A5'],
    'B': ['B1', 'B2', 'B3', 'B4', 'B5'],
    'C': ['C1', 'C2', 'C3', 'C4', 'C5'],
    'D': ['D1', 'D2', 'D3', 'D4', 'D5'],
    'E': ['E1', 'E2', 'E3', 'E4', 'E5']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Remove all columns between column name 'B' to 'D'
df.drop(df.loc[:, 'B':'D'].columns, axis=1)


Output :

    A   E
0  A1  E1
1  A2  E2
2  A3  E3
3  A4  E4
4  A5  E5

Note: Different loc() and iloc() is iloc() exclude last column range element.   

Pandas Drop Columns from Datafram using Iterative Method 

In this example Delete columns between specific column names as the below code creates a Pandas DataFrame from a dictionary and iterates through its columns. For each column, if the letter ‘A’ is present in the column name, that column is deleted from the DataFrame. The resulting modified DataFrame is displayed.

Python3




# Import pandas package
import pandas as pd
 
# create a dictionary with five fields each
data = {
    'A': ['A1', 'A2', 'A3', 'A4', 'A5'],
    'B': ['B1', 'B2', 'B3', 'B4', 'B5'],
    'C': ['C1', 'C2', 'C3', 'C4', 'C5'],
    'D': ['D1', 'D2', 'D3', 'D4', 'D5'],
    'E': ['E1', 'E2', 'E3', 'E4', 'E5']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
for col in df.columns:
    if 'A' in col:
        del df[col]
 
df


Output :

    B   C   D   E
0  B1  C1  D1  E1
1  B2  C2  D2  E2
2  B3  C3  D3  E3
3  B4  C4  D4  E4
4  B5  C5  D5  E5

Dataframe Drop Column in Pandas using Dataframe.pop() Method

In this example Removing a Specific Column from a DataFrame as the code demonstrates how to remove a specific column (‘B’) from a Pandas DataFrame created from a dictionary. It uses the pop method, and the resulting modified DataFrame is displayed.

Python3




# Import pandas package
import pandas as pd
 
# create a dictionary with five fields each
data = {
    'A': ['A1', 'A2', 'A3', 'A4', 'A5'],
    'B': ['B1', 'B2', 'B3', 'B4', 'B5'],
    'C': ['C1', 'C2', 'C3', 'C4', 'C5'],
    'D': ['D1', 'D2', 'D3', 'D4', 'D5'],
    'E': ['E1', 'E2', 'E3', 'E4', 'E5']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
df.pop('B')
 
df


Output:

A    C    D    E
0    A1    C1    D1    E1
1    A2    C2    D2    E2
2    A3    C3    D3    E3
3    A4    C4    D4    E4
4    A5    C5    D5    E5




Last Updated : 07 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads