Drop columns in DataFrame by label Names or by Index Positions

02 Jul, 2020
In this article, we will discuss how to drop columns in Pandas Dataframe by label Names or by Index Positions. Drop columns from a DataFrame can be achieved in multiple ways.
Let’s create a simple dataframe with a dictionary of lists, say column names are: ‘Name’, ‘Age’, ‘Place’, ‘College’.
# import pandas library as pdimport pandas as pd  # List of Tuplesstudents = [('Ankit', 22, 'Up', 'Geu'),           ('Ankita', 31, 'Delhi', 'Gehu'),           ('Rahul', 16, 'Tokyo', 'Abes'),           ('Simran', 41, 'Delhi', 'Gehu'),           ('Shaurya', 33, 'Delhi', 'Geu'),           ('Harshita', 35, 'Mumbai', 'Bhu' ),           ('Swapnil', 35, 'Mp', 'Geu'),           ('Priya', 35, 'Uk', 'Geu'),           ('Jeet', 35, 'Guj', 'Gehu'),           ('Ananya', 35, 'Up', 'Bhu')            ]  # Create a DataFrame object from# list of tuples with columns# and indices.details = pd.DataFrame(students,                       columns =['Name', 'Age',                                  'Place', 'College'],                        index =['a', 'b', 'c', 'd', 'e',                                 'f', 'g', 'i', 'j', 'k'])# show the dataframedetails
Output :
Method 1: Drop Columns from a Dataframe using dataframe.drop() method.Example 1: Remove specific single mention column.
# import pandas library as pdimport pandas as pd  # List of Tuplesstudents = [('Ankit', 22, 'Up', 'Geu'),           ('Ankita', 31, 'Delhi', 'Gehu'),           ('Rahul', 16, 'Tokyo', 'Abes'),           ('Simran', 41, 'Delhi', 'Gehu'),           ('Shaurya', 33, 'Delhi', 'Geu'),           ('Harshita', 35, 'Mumbai', 'Bhu' ),           ('Swapnil', 35, 'Mp', 'Geu'),           ('Priya', 35, 'Uk', 'Geu'),           ('Jeet', 35, 'Guj', 'Gehu'),           ('Ananya', 35, 'Up', 'Bhu')            ]  # Create a DataFrame object from# list of tuples with columns# and indices.details = pd.DataFrame(students,                        columns =['Name', 'Age',                                 'Place', 'College'],                       index =['a', 'b', 'c', 'd', 'e',                               'f', 'g', 'i', 'j', 'k'])  # Remove column name 'Age' rslt_df = details.drop(['Age'],                       axis = 1)# show the dataframerslt_df
Output :
Example 2 : Remove specific multiple mentions columns.
# import pandas library as pdimport pandas as pd  # List of Tuplesstudents = [('Ankit', 22, 'Up', 'Geu'),           ('Ankita', 31, 'Delhi', 'Gehu'),           ('Rahul', 16, 'Tokyo', 'Abes'),           ('Simran', 41, 'Delhi', 'Gehu'),           ('Shaurya', 33, 'Delhi', 'Geu'),           ('Harshita', 35, 'Mumbai', 'Bhu' ),           ('Swapnil', 35, 'Mp', 'Geu'),           ('Priya', 35, 'Uk', 'Geu'),           ('Jeet', 35, 'Guj', 'Gehu'),           ('Ananya', 35, 'Up', 'Bhu')            ]  # Create a DataFrame object from# list of tuples with columns# and indices.details = pd.DataFrame(students,                        columns =['Name', 'Age',                                  'Place', 'College'],                       index =['a', 'b', 'c', 'd', 'e',                               'f', 'g', 'i', 'j', 'k'])  # Remove two columns name is 'Age' and # 'College' rslt_df = details.drop(['Age', 'College'],                       axis = 1)# show the dataframerslt_df
Output :
Example 3: Remove columns as based on column index.
# import pandas library as pdimport pandas as pd  # List of Tuplesstudents = [('Ankit', 22, 'Up', 'Geu'),           ('Ankita', 31, 'Delhi', 'Gehu'),           ('Rahul', 16, 'Tokyo', 'Abes'),           ('Simran', 41, 'Delhi', 'Gehu'),           ('Shaurya', 33, 'Delhi', 'Geu'),           ('Harshita', 35, 'Mumbai', 'Bhu' ),           ('Swapnil', 35, 'Mp', 'Geu'),           ('Priya', 35, 'Uk', 'Geu'),           ('Jeet', 35, 'Guj', 'Gehu'),           ('Ananya', 35, 'Up', 'Bhu')            ]  # Create a DataFrame object from# list of tuples with columns# and indices.details = pd.DataFrame(students,                       columns =['Name', 'Age',                                  'Place', 'College'],                       index =['a', 'b', 'c', 'd', 'e',                               'f', 'g', 'i', 'j', 'k'])  # Remove three columns as index base# 0, 1, 2rslt_df = details.drop(details.columns[[0, 1, 2]],                       axis = 1)  # show the dataframerslt_df
Output :
Method 2: Drop Columns from a Dataframe using iloc[] and drop() method.
Example: Remove all columns between a specific column to another columns(exclude)
# import pandas library as pdimport pandas as pd  # List of Tuplesstudents = [('Ankit', 22, 'Up', 'Geu'),           ('Ankita', 31, 'Delhi', 'Gehu'),           ('Rahul', 16, 'Tokyo', 'Abes'),           ('Simran', 41, 'Delhi', 'Gehu'),           ('Shaurya', 33, 'Delhi', 'Geu'),           ('Harshita', 35, 'Mumbai', 'Bhu' ),           ('Swapnil', 35, 'Mp', 'Geu'),           ('Priya', 35, 'Uk', 'Geu'),           ('Jeet', 35, 'Guj', 'Gehu'),           ('Ananya', 35, 'Up', 'Bhu')            ]  # Create a DataFrame object from# list of tuples with columns# and indices.details = pd.DataFrame(students,                        columns =['Name', 'Age',                                  'Place', 'College'],                       index =['a', 'b', 'c', 'd', 'e',                                'f', 'g', 'i', 'j', 'k'])  # Remove all columns from column# index 1 to 3(exclude) rslt_df = details.drop(details.iloc[:, 1:3],                       axis = 1)  # show the dataframerslt_df
Output :
Method 3: Drop Columns from a Dataframe using loc[] and drop() method.Example: Remove all columns between a specific column name to another columns name.
# import pandas library as pdimport pandas as pd  # List of Tuplesstudents = [('Ankit', 22, 'Up', 'Geu'),           ('Ankita', 31, 'Delhi', 'Gehu'),           ('Rahul', 16, 'Tokyo', 'Abes'),           ('Simran', 41, 'Delhi', 'Gehu'),           ('Shaurya', 33, 'Delhi', 'Geu'),           ('Harshita', 35, 'Mumbai', 'Bhu' ),           ('Swapnil', 35, 'Mp', 'Geu'),           ('Priya', 35, 'Uk', 'Geu'),           ('Jeet', 35, 'Guj', 'Gehu'),           ('Ananya', 35, 'Up', 'Bhu')            ]  # Create a DataFrame object from# list of tuples with columns# and indices.details = pd.DataFrame(students,                        columns =['Name', 'Age',                                  'Place', 'College'],                       index =['a', 'b', 'c', 'd', 'e',                                'f', 'g', 'i', 'j', 'k'])  # Remove all columns from column name # 'Name' to 'College' rslt_df = details.drop(details.loc[:, 'Name':'College'].columns,                       axis = 1)  # show the dataframe# only indexes printrslt_df
Output :
Note: Different loc() and iloc() is iloc() exclude last column range element.
Method 4: Drop Columns from a Dataframe by iterative way.Example: Remove specific column.
# import pandas library as pdimport pandas as pd  # List of Tuplesstudents = [('Ankit', 22, 'Up', 'Geu'),           ('Ankita', 31, 'Delhi', 'Gehu'),           ('Rahul', 16, 'Tokyo', 'Abes'),           ('Simran', 41, 'Delhi', 'Gehu'),           ('Shaurya', 33, 'Delhi', 'Geu'),           ('Harshita', 35, 'Mumbai', 'Bhu' ),           ('Swapnil', 35, 'Mp', 'Geu'),           ('Priya', 35, 'Uk', 'Geu'),           ('Jeet', 35, 'Guj', 'Gehu'),           ('Ananya', 35, 'Up', 'Bhu')            ]  # Create a DataFrame object from# list of tuples with columns# and indices.details = pd.DataFrame(students,                        columns =['Name', 'Age',                                  'Place', 'College'],                       index =['a', 'b', 'c', 'd', 'e',                               'f', 'g', 'i', 'j', 'k'])  # loop throughout all the columnsfor column in details.columns :    if column == 'Age' :                  # delete the column        del details[column]          # show the dataframedetails
Output :
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON