Drop columns in DataFrame by label Names or by Index Positions

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’.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('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 dataframe
details

chevron_right


Output :
dataframe image

Method 1: Drop Columns from a Dataframe using dataframe.drop() method.
Example 1: Remove specific single mention column.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('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 dataframe
rslt_df

chevron_right


Output :
drop column from dataframe - 1



Example 2 : Remove specific multiple mentions columns.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('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 dataframe
rslt_df

chevron_right


Output :
drop column from dataframe - 2

Example 3: Remove columns as based on column index.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('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, 2
rslt_df = details.drop(details.columns[[0, 1, 2]],
                       axis = 1)
  
# show the dataframe
rslt_df

chevron_right


Output :
drop column from dataframe - 3

Method 2: Drop Columns from a Dataframe using iloc[] and drop() method.

Example: Remove all columns between a specific column to another columns(exclude)

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('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 dataframe
rslt_df

chevron_right


Output :
drop column from dataframe - 4

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('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 print
rslt_df

chevron_right


Output :
drop column from dataframe - 6

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('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 columns
for column in details.columns :
    if column == 'Age' :
          
        # delete the column
        del details[column]
          
# show the dataframe
details

chevron_right


Output :
drop column from dataframe - 7

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.