Drop Columns in DataFrame by Label Names or by Index Positions
Last Updated :
18 Nov, 2024
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:
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 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:
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
Similar Reads
How to drop rows in Pandas DataFrame by index labels? Dropping rows in a Pandas DataFrame by index labels is a common operation when you need to remove specific rows based on their index positions. For example, we are working with a DataFrame that contains details of students, including their names, ages, and universities. Initially, the DataFrame has
5 min read
How to drop one or multiple columns in Pandas DataFrame Let's learn how to drop one or more columns in Pandas DataFrame for data manipulation. Drop Columns Using df.drop() MethodLet's consider an example of the dataset (data) with three columns 'A', 'B', and 'C'. Now, to drop a single column, use the drop() method with the columnâs name.Pythonimport pand
4 min read
How to Select Rows & Columns by Name or Index in Pandas Dataframe - Using loc and iloc When working with labeled data or referencing specific positions in a DataFrame, selecting specific rows and columns from Pandas DataFrame is important. In this article, weâll focus on pandas functionsâloc and ilocâthat allow you to select rows and columns either by their labels (names) or their int
4 min read
Split Pandas Dataframe by Column Index Pandas support two data structures for storing data the series (single column) and dataframe where values are stored in a 2D table (rows and columns).  To index  a  dataframe using the index we need to make use of dataframe.iloc() method which takes Syntax: pandas.DataFrame.iloc[] Parameters:Index
3 min read
How to Sort a Pandas DataFrame by Both Index and Column? In this article, we will discuss how to sort a Pandas dataframe by both index and columns. Sort DataFrame based on IndexWe can sort a Pandas DataFrame based on Index and column using sort_index method. To sort the DataFrame based on the index we need to pass axis=0 as a parameter to sort_index metho
3 min read