Drop column in pandas python

Delete or drop column in python pandas by done by using drop() function. Here we will focus on Drop single and multiple columns in pandas using index (iloc() function), column name(ix() function) and by position. Drop column name that starts with, ends with, contains a character and also with regular expression and like% function. Let’s see example of each.

  • Drop or delete column in pandas by column name using drop() function.
  • Drop single and multiple columns in pandas by using column index .
  • Drop or delete multiple columns between two column index using iloc() function.
  • Drop multiple columns between two column names using loc() and ix() function.
  • Drop column name which starts with, ends with and contains a character.
  • Drop by column name using regular expression.

First let’s create dataframe

Create Dataframe

import pandas as pd
import numpy as np
 
#Create a DataFrame
d = {
    'Name':['Alisa','Bobby','jodha','jack','raghu','Cathrine',
            'Alisa','Bobby','kumar','Alisa','Alex','Cathrine'],
    'Country' : ["USA","UK","Germany","USA","India","France","USA","UK","India","USA","Canada","France"],
    'Age':[26,24,23,22,23,24,26,24,22,23,24,24],
    'Score':[85,63,55,74,31,77,85,63,42,62,89,77],
    'Scholarship':['Yes','No','Yes','Yes','Yes','No','No','Yes','No','No','Yes','Yes']}
 
df = pd.DataFrame(d,columns=['Name','Country','Age','Score','Scholarship'])
df

The resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 1

 

Delete or drop column in pandas by column name using drop() function
Let’s see an example of how to drop a column by name in python pandas

# drop a column based on name

df.drop('Age',axis=1)

The above code drops the column named ‘Age’, the argument axis=1 denotes column, so the resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 2

 

Drop single column in pandas by using column index

Let’s see an example on dropping the column by its index in python pandas


# drop a column based on column index

df.drop(df.columns[3],axis=1)

In the above example column with index 3 is dropped(4th  column). So the resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 3

 

 

Delete a column based on column name:


# delete a column

del df['Age']
df

In the above example column with the name ‘Age’ is deleted. So the resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 4

 

 

Drop multiple columns based on column name in pandas

Let’s see an example of how to drop multiple columns by name in python pandas


''' drop multiple column based on name'''
df.drop(['Age', 'Score'], axis = 1)

The above code drops the columns named ‘Age’ and ’Score’.  The argument axis=1 denotes column, so the resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 5

 

 

Drop multiple columns based on column index in pandas

Let’s see an example of how to drop multiple columns by index.


''' drop multiple columns based on column index'''
df.drop(df.columns[[1,3]], axis = 1)

In the above example column with index 1 (2nd column) and Index 3 (4th column) is dropped. So the resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 6

 

 

Drop multiple columns between two column index in pandas
Let’s see an example of how to drop multiple columns between two index using iloc() function

''' Remove columns between two column using index - using iloc() '''

df.drop(df.iloc[:, 1:3], axis = 1)

In the above example column with index 1 (2nd column) and Index 2 (3rd column) is dropped. So the resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 7

 

Drop multiple columns between two column names in pandas

Let’s see an example of how to drop multiple columns between two column name using ix() function and loc() function


''' Remove columns between two column using column name - using ix() '''

df.drop(df.ix[:, 'Country':'Score'].columns, axis = 1)

OR


''' Remove columns between two column using column name - using loc() '''

df.drop(df.loc[:, 'Country':'Score'].columns, axis = 1)

In the above example column name starting from “country” ending till “score” is removed. So the resultant dataframe with 3 columns removed will be

Delete or drop column in python pandas – drop multiple column 8

 

 

Drop multiple columns that starts with character in pandas

Let’s see an example of how to drop multiple columns that starts with a character in pandas using loc() function


''' drop column name starts with a character '''

df.loc[:,~ df.columns.str.startswith('A')]

In the above example column name starting with “A” will be dropped. So the resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 9

 

 

Drop multiple columns that ends with character in pandas

Let’s see an example of how to drop multiple columns that ends with a character using loc() function

''' drop column name ends with a character'''

df.loc[:,~df.columns.str.endswith('e')]

In the above example column name ending with “e” will be dropped. So the resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 10

 

 

Drop multiple columns that contains a character (like%) in pandas

Let’s see an example of how to drop multiple columns that contains a character (like%) in pandas using loc() function

''' drop column name contains ---- drop column name like% in''' 

df.loc[:,~df.columns.str.contains('sc',case =False)]

In the above example column name that contains “sc” will be dropped. case=False indicates column dropped irrespective of case. So the resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 11

 

 

Drop columns using regular expression in pandas – regex

Let’s see an example of how to drop columns using regular expressions – regex.


''' drop column name using regular expression ''' 

df[df.columns.drop(list(df.filter(regex="(Sc)+?.+")))]

In the above example column starts with “sc” will be dropped using regular expressions. So the resultant dataframe will be

Delete or drop column in python pandas – drop multiple column 12

 

previous-small Drop or delete columns in python pandas                                                                                                           next_small Drop or delete columns in python pandas