Open In App

Drop Columns in DataFrame by Label Names or by Index Positions

Last Updated : 18 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

This article will discuss dropping columns in Pandas DataFrame by label names or index positions. Dropping columns can be achieved using methods like drop(), iloc[], loc[], and iterative approaches. Let’s explore these methods with examples.

Drop Columns by Label Names or Index Positions Using drop()

The simplest way to drop columns by their label names is by using the drop() method. This method allows you to specify the column labels you wish to remove.

DataFrame.drop(labels=None, axis=0, index=None, columns=None, inplace=False)

To drop columns, set the axis parameter to 1 or use the columns parameter.

Let's consider an example:

Python
import pandas as pd

# Creating the dataset
data = {
    'Name': ['Rachel', 'Ross', 'Monica', 'Chandler', 'Phoebe', 'Joey'],
    'Age': [30, 32, 31, 33, 34, 31],
    'Profession': ['Fashion Designer', 'Paleontologist', 'Chef', 'Advertiser', 'Musician', 'Actor'],
    'Favorite_Coffee': ['Latte', 'Black Coffee', 'Cappuccino', 'Espresso', 'Herbal Tea', 'Mocha']
}

df = pd.DataFrame(data)

print("Original Dataset:")
display(df)

Output:

df

Dropping Columns by Label Names using drop()

Let's remove the Profession and Favorite_Coffee columns.

Python
# Dropping columns by label names
df_dropped_labels = df.drop(columns=['Profession', 'Favorite_Coffee'])

print("\nDataset after dropping 'Profession' and 'Favorite_Coffee':")
display(df_dropped_labels)

Output:

dropping-columns-by-label-names

Dropping Columns by Index Positions

Using index positions to drop the same columns (Profession and Favorite_Coffee).

Python
# Dropping columns by index positions
columns_to_drop = [2, 3]  # Index positions of 'Profession' and 'Favorite_Coffee'
df_dropped_indices = df.drop(df.columns[columns_to_drop], axis=1)

print("\nDataset after dropping columns by index positions:")
display(df_dropped_indices)

Output:

dropping-columns-by-Index-Positions

Drop Columns Using Index Positions with iloc[]

If you don’t know the column labels but know their positions, you can drop columns using index positions.

Python
import pandas as pd

# Creating the dataset
data = {
    'Name': ['Rachel', 'Ross', 'Monica', 'Chandler', 'Phoebe', 'Joey'],
    'Age': [30, 32, 31, 33, 34, 31],
    'Profession': ['Fashion Designer', 'Paleontologist', 'Chef', 'Advertiser', 'Musician', 'Actor'],
    'Favorite_Coffee': ['Latte', 'Black Coffee', 'Cappuccino', 'Espresso', 'Herbal Tea', 'Mocha']
}

df = pd.DataFrame(data)

# Changing the index to 'A', 'B', 'C', etc.
df.index = ['A', 'B', 'C', 'D', 'E', 'F']

print("Original Dataset:")
print(df)

# Drop the 1st and 3rd columns (index 0 and 2)
df_dropped = df.drop(df.columns[[0, 2]], axis=1)
print(df_dropped)

Output:

Original Dataset:
Name Age Profession Favorite_Coffee
A Rachel 30 Fashion Designer Latte
B Ross 32 Paleontologist Black Coffee
C Monica 31 Chef Cappuccino
D Chandler 33 Advertiser Espresso
E Phoebe 34 Musician Herbal Tea
F Joey 31 Actor Mocha
   Age Favorite_Coffee
A 30 Latte
B 32 Black Coffee
C 31 Cappuccino
D 33 Espresso
E 34 Herbal Tea
F 31 Mocha

Drop Columns Using Slicing with loc[]

The loc[] method allows you to drop a range of columns by their label names. Let's take an example to remove column between two labels, we will be using the same dataset that we have used above:

Python
# Drop columns from 'Age' to 'Favorite_Coffee'
df_dropped = df.drop(df.loc[:, 'Age':'Favorite_Coffee'].columns, axis=1)
print(df_dropped)

Output:

        Name
A Rachel
B Ross
C Monica
D Chandler
E Phoebe
F Joey

Drop Columns Iteratively

If you need to remove columns conditionally, you can use an iterative approach.

Python
# Remove any column where the name is 'Age'
for col in df.columns:
    if col == 'Age':
        del df[col]
        
print(df)

Output:

       Name        Profession Favorite_Coffee
A Rachel Fashion Designer Latte
B Ross Paleontologist Black Coffee
C Monica Chef Cappuccino
D Chandler Advertiser Espresso
E Phoebe Musician Herbal Tea
F Joey Actor Mocha



Next Article
Practice Tags :

Similar Reads