How to drop one or multiple columns in Pandas Dataframe - GeeksforGeeks

21 May, 2021
Let’s discuss how to drop one or multiple columns in Pandas Dataframe. Drop one or more than one columns from a DataFrame can be achieved in multiple ways.
Create a simple dataframe with dictionary of lists, say column names are A, B, C, D, E.
# Import pandas package import pandas as pd  # create a dictionary with five fields eachdata = {    '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:
YouTubeGeeksforGeeks507K subscribersHow to Remove Columns From Pandas Dataframe? | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 9:07•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=8PN1eXQGZ9c" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Method #1: Drop Columns from a Dataframe using drop() method.
Remove specific single column.
# 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:
 Remove specific multiple columns.
# Import pandas package import pandas as pd  # create a dictionary with five fields eachdata = {    '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: 
Remove columns as based on column index.
# Import pandas package import pandas as pd  # create a dictionary with five fields eachdata = {    '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 basedf.drop(df.columns[[0, 4, 2]], axis = 1, inplace = True)  df
Output:
 
Method #2: Drop Columns from a Dataframe using iloc[] and drop() method.
Remove all columns between a specific column to another columns.
# 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 3df.drop(df.iloc[:, 1:3], inplace = True, axis = 1)  df
Output: 
Method #3: Drop Columns from a Dataframe using ix() and drop() method.
Remove all columns between a specific column name to another columns name.
# 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:
 Method #4: Drop Columns from a Dataframe using loc[] and drop() method.
Remove all columns between a specific column name to another columns name.
# 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:Note: Different loc() and iloc() is iloc() exclude last column range element.
 Method #5: Drop Columns from a Dataframe by iterative way.
Remove all columns between a specific column name to another columns name.
# 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:
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Technical Scripter 2018
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
Reading and Writing to text files in Python
*args and **kwargs in Python
Create a Pandas DataFrame from Lists