Pandas Delete Column

In this Python Pandas tutorial, we will discuss everything on Pandas Delete column and how to Drop column in DataFrame using Pandas.

  • Pandas Delete Column DataFrame
  • Pandas Delete Column by Name
  • Pandas Delete Column by Index
  • Pandas Delete Column if Exists
  • Pandas Delete Column by Condition
  • Pandas Delete Columns with NaN
  • Pandas Delete Column if all nan
  • Pandas Delete Column by Position
  • Pandas Delete Column with no Name
  • Pandas Delete Column Header
  • Pandas Delete Columns Except
  • Drop first column in Pandas DataFrame
  • Drop last column in Pandas DataFrame
  • Drop multiple columns in Pandas DataFrame
  • Drop duplicate columns in Pandas DataFrame
  • Remove first column in Pandas DataFrame
  • Remove column names in Pandas DataFrame
  • Drop column Pandas series
  • Drop one column in Pandas DataFrame
  • Drop list of column in Pandas DataFrame

We have used Electric Car Dataset downloaded from Kaggle.

Pandas Delete Column DataFrame

In this section, we will learn about Pandas Delete Column from DataFrame using Python.

  • There are three methods of removing column from DataFrame in Python Pandas. drop(), delete(), pop().
  • dop() is the mostly used method in Python Pandas for removing rows or columns and we will be using the same.

Syntax:

This is the the syntax for drop() method in Python Pandas.

df.drop(
    labels=None,
    axis: 'Axis' = 0,
    index=None,
    columns=None,
    level: 'Level | None' = None,
    inplace: 'bool' = False,
    errors: 'str' = 'raise',
)
  • labels – provide the name(s) of row(s) or column(s)
  • axis – 1 is for row and 0 is for column, if label is column name then provide axis=1.
  • index – axis 0 = label, provide index if working with rows
  • columns – axis=1, columns=labels
  • inplace – if set to True, then changes will take place immediately. value reassignment will not be required.
  • erros – if set to ‘raise’ then error will appear when something will go wrong.

In our example on the jupyter notebook, we have demonstrated all of these methods.

Read: How to Convert Pandas DataFrame to a Dictionary

Pandas Delete Column by Name

In this section, we will learn about Pandas Delete Column by Name.

  • Raw dataset has huge amount of data, not every information is necessary for a particular task or prediction model.
  • At that time, we clean the dataset by removing unwanted rows and columns.
  • drop() method is used to remove the columns.
  • single column name or list of column name can be passed to delete the column by name.
  • In our example, we have deleted ‘PlugType’ column from the dataframe.
  • Here is the main code that is deleting the column by name.
df.drop('PlugType', axis=1, inplace=True)

Implementation on Jupyter Notebook

Please read the comments to understand the use of that code snippet.

Read: Python Pandas Drop Rows Example

Pandas Delete Column by Index

In this section, we will learn about Pandas Delete Column by index.

  • While cleaning the data at time, we came across column(s) that has no name. At that time, using index value we can delete the column.
  • Using drop method we can provide the index number for the column. This will delete the column from the dataframe.
  • In our example, we have delete the column on index 0. But you can change it to any value but that index but be present in the dataset.
  • inplace = True, means the changes will take effect immedietley. There is no need for re-assignment.
  • Here is the main code responsible for deleting the column by index in Python Pandas.
df.drop(df.columns[0], axis=1, inplace=True)

Implementation on Jupyter Notebook

Please read the comments to understand the use of that code snippet.

Read: How to use Pandas drop() function

Pandas Delete Column if Exists

In this section, we will learn about Pandas delete column if exists.

  • If exists condition here simply means that delete the column if that column is present in the dataframe.
  • there are multiple ways of doing so but the most efficient way is by setting the errors to ‘ignore’.
  • If the column is present than it will be deleted otherwise nothing will happen.
  • In our example, we have deleted two columns. Out of these two columns only one is present in the dataset.
  • Here is the main code to delete the column if exist.
df.drop(['Model', 'test'], axis=1, errors='ignore', inplace=True)

Implementation on Jupyter Notebook

Please refer to the examples to understand the use of code snippets.

Read: Groupby in Python Pandas

Pandas Delete Column by Condition

In this section, we will learn about Pandas Delete Column by Condition.

  • While cleaning the the dataset at times we have to remove part of data depending upon some condition.
  • Let’s say we are working on the tax payers in USA dataset. Then we will apply a condition to seperate non-tax payer based apon their annual income. After removing non-tax payer will be left with data of tax payers in USA.
  • In our example, we have removed all the cars that are below 50000 euro.
  • Here is the main code to delete column by condition.
df.drop(df[df['PriceEuro'] < 50000].index, inplace = True)

Implement on Jupyter Notebook

Read: Crosstab in Python Pandas

Pandas Delete Columns with NaN

In this section, we will learn about Pandas Delete Columns with NaN.

  • NaN referrs to the missing values in the dataset.
  • Missing values are the most common thing that can be found in the dataset.
  • image a company shared a feedback form with the customer and customer submits the form without filling it or just by filling mandatory fields. Now this data will be saved as NaN in the database.
  • In python pandas we can delete the missing values using dropna() method.
  • dropna() is specially written to find and delete the rows or columns with the missing value(s).
  • Since we want to delete columns so we will provide axis=1 in dropna() function.
  • In our example, we have deleted all the columns that have even 1 missing value.
  • Here is the main code to delete the column with NaN or misisng values.
df.dropna(axis=1, inplace=True)

Implementation on Jupyter Notebook

Read: Missing Data in Pandas in Python

Pandas Delete Column if all nan

In this section, we will learn about Pandas Delete Column if all nan.

  • NaN referrs to the missing values in the dataset.
  • Missing values are the most common thing that can be found in the dataset.
  • image a company shared a feedback form with the customer and customer submits the form without filling it or just by filling mandatory fields. Now this data will be saved as NaN in the database.
  • In python pandas we can delete the missing values using dropna() method.
  • dropna() is specially written to find and delete the rows or columns with the missing value(s).
  • Since we want to delete column(s) having all the missing value so we will using df.dropna(axis=1, how='all')
  • how=’all’ checks if the all the values of column are empty. If yes then it deletes the column.
  • In our example, we have created new column ‘Rating’ with no values in it. So this column will be deleted after executing this code snippet.
  • Here is the main code to delete the column with all NaN or misisng values.

Read: Python Pandas CSV Tutorial

Pandas Delete Column by Position

In this section, we will understand about Pandas Delete column by Position. Position can also be referred to as an index.

  • While cleaning the data at a time, we came across column(s) that has no name. At that time, using the index value we can delete the column.
  • Using the drop method we can provide the index number for the column. This will delete the column from the dataframe.
  • In our example, we have deleted the column on index 0. But you can change it to any value but that index but be present in the dataset.
  • inplace = True, which means the changes will take effect immediately. There is no need for re-assignment.
  • Here is the main code responsible for deleting the column by index in Python Pandas.

Read: Pandas DataFrame Iterrows

Pandas Delete Column with no Name

In this section, we will learn about Pandas Delete Column with No Name.

  • Columns with no name can controlled or operated using their index value.
  • In our example, we whave created a new column with no name.
  • to delete that column we have used it’s index value in pandas drop method.
  • Here is the main code to Delete Column with No Name.
df.drop(df.columns[14], axis=1, inplace=True)

Implementation on Jupyter Notebook

Pandas Delete Column Header

In this section, we will learn about Pandas delete column header in Python.

  • It is not possibel to remove the header from the dataset using Python Pandas but it can hide in multiple ways.
  • first method is change the header to empty string for all the columns.
  • second method is export to new file with header=False .
  • In our example, we have demonstrated bothe ways.
  • Here is the main code for removing Header in Python pandas.
# set the columns to empty string
df.columns = [''] * len(df.columns)

# export file with no header
df.to_csv('without_header.csv', header=False)

Implementation using Jupyter Notebook

Read How to Convert Python DataFrame to JSON

Pandas Delete Columns Except

In this section, we will learn about Pandas Delete Columns Except.

  • Using difference method we can exclude columns that we don’t want to delete.
  • In our example, we have excluded Model and PowerTrain.
  • Here is the main code to delete columns with exception.
df.drop(df[df.columns.difference(['Model', 'PowerTrain'])], axis=1)

Implementation on Jupyter Notebook

Read How to convert floats to integer in Pandas

Drop column in Pandas DataFrame

  • In this Program, we will discuss how to drop column in Pandas Dataframe.
  • In Python, Pandas drop columns and rows from DataFrame. You can use the “drop” method and this function specifies labels from columns or rows. The Pandas.drop() method deletes columns and rows by directly mentioning the column names or indexes.

Syntax:

Here is the Syntax of Pandas.drop() method

DataFrame.drop
              (
               Labels=None,
               axis=0,
               index=None,
               columns=None,
               Level=None,
               inplace=False,
               errors='raise'
              )
  • It consists of few Parameters
    • Labels: It is a column name to drop and by default, it is set as None.
    • axis: This parameter indicates whether to drop labels if axis=1 then it removes columns and by default, it takes 0 value.
    • index: This parameter specifies the axis and provides the row label.
    • Columns: It accepts a single column label and by default, it is set as None value.

Source Code:

import pandas as pd

new_dict = {
	'val1':['m', 'o', 'a', 'p', 'z'],
	'val2':['z', 'm', 'u', 'x', 'y'],
	'val3':['11', '55', '806', '22', '66'],
	'val4':['389', '32', '180', '378', '674'],
	'val5':['134', '809', '457', '293', '3939'] }

df = pd.DataFrame(new_dict)
C= df.drop(['val2'], axis = 1)
print(C)

In the above code first, we have imported a Pandas package and then create a dictionary in which we have inserted five fields in each column.

Now we want to convert the dictionary into a dataframe by using the pd.Dataframe() method.

Now in this example, we want to remove the column name ‘val2’. To do this we can use the df.drop() method to remove columns for the dataframe.

You can refer to the below Screenshot

Drop column in Pandas DataFrame
Drop column in Pandas DataFrame

Read How to Get first N rows of Pandas DataFrame in Python

Drop first column in Pandas DataFrame

  • Here we can see how to drop the first column of Pandas DataFrame in Python.
  • By using the df.iloc() method we can select a part of the Pandas DataFrame based on the indexing. In Python Pandas the iloc() method is used to select a specific cell of the Dataset and this method accepts only integer values and also we cannot pass boolean values as an index.

Syntax:

Here is the Syntax of DataFrame.iloc() method

DataFrame.iloc()

Example:

Let’s take an example and understand how to drop the first column from Dataframe.

import pandas as pd

new_lis = [('Micheal',  245, 'Newzealand',   34) ,
            ('John',    968, 'Switzerland' ,  25) ,
            ('Geroge',  1678, 'Australia',   36) ,
            ('Oliva',   3789, 'Bangladesh' ,  19)]

df = pd.DataFrame(  new_lis, 
                    columns=['Stu_name', 'Stu_id', 'Stu_city', 'Stu_age'])

new_data = df.iloc[: , 1:]
print("Updated Dataframe after removing first col : ")
print(new_data)

Here is the Screenshot of the following given code

Drop first column in Pandas DataFrame
Drop the first column in Pandas DataFrame

In the above Screenshot as you can see the output of the first column ‘stu_name’ has been removed from DataFrame.

Read Pandas replace nan with 0

Drop the first column from DataFrame

By using the df.drop() method we can perform this particular task and in this example, we will use parameter axis=1 and inplace =True.

Source Code:

import pandas as pd

new_dict = {
	'col1':['167', '267', '390', '789', '129'],
	'col2':['209', '108', '329', '458', '788'],
	'col3':['267', '589', '349', '1589', '2789'],
	'col4':['44', '99', '31', '11', '23'],
	'col5':['72', '44', '68', '908', '502'] }


df = pd.DataFrame(new_dict)
df.drop(df.columns[[0]], axis = 1, inplace = True)
print(df)

In the above code, we have dropped the first column based on the column index. In this example, we have mentioned the index number along with the axis and inplace parameter in the df.drop() method.

Once you will print ‘df’ then the output will show the modifying dataframe.

Here is the implementation of the following given code

Drop first column in Pandas DataFrame
Drop the first column in Pandas DataFrame

Read How to Add a Column to a DataFrame in Python Pandas

Drop last column in Pandas DataFrame

  • Let us see how to drop the last column of Pandas DataFrame.
  • By using the del keyword we can easily drop the last column of Pandas DataFrame. In Python, the del keyword is used to remove the variable from namespace and delete an object like lists and it does not return any type of value.
  • To drop the last column of the dataframe first, we set the position at -1 and then select the column bypassing the column name in the del method.

Syntax:

del obj_name

Example:

Let’s take an example and check how to drop the last column of the DataFrame

import pandas as pd

new_lis = [('Micheal',  245, 'Newzealand',   34) ,
            ('John',    968, 'Switzerland' ,  25) ,
            ('Geroge',  1678, 'Australia',   36) ,
            ('Oliva',   3789, 'Bangladesh' ,  19)]

df = pd.DataFrame(  new_lis, 
                    columns=['Stu_name', 'Stu_id', 'Stu_city', 'Stu_age'])
del df[df.columns[-1]]
print("Updated Dataframe after drop last column : ")
print(df)

In the above code, we have created a list of tuples and then create a Dataframe object. Now we want to drop the last column of the dataframe we can simply apply the df.columns[-1] method in the del keyword.

Here is the execution of the following given code

Drop last column in Pandas DataFrame
Drop last column in Pandas DataFrame

Read How to Convert Pandas DataFrame to NumPy Array in Python

Drop the last column of Pandas DataFrame

By using the df.drop() method we can solve this problem and in this example, we have mentioned the index number along with the axis and inplace parameter in the df.drop() method.

Source Code:

import pandas as pd

new_dict = {
	'val1':['457', '289', '180', '743', '119'],
	'val2':['278', '140', '678', '712', '8924'],
	'val3':['267', '589', '349', '1589', '2789'],
	'val4':['44', '99', '31', '11', '23'],
	'val5':['72', '44', '68', '908', '502'] }


df = pd.DataFrame(new_dict)
df.drop(df.columns[[4]], axis = 1, inplace = True)
print(df)

Here is the Screenshot of the following given code

Drop last column in Pandas DataFrame
Drop last column in Pandas DataFrame

As you can see in the Screenshot the last column ‘val5’ has been removed from Pandas DataFrame.

Read How to Find Duplicates in Python DataFrame

Drop multiple columns in Pandas DataFrame

  • Let us see how to drop multiple columns in Pandas DataFrame.
  • In this example we will apply the method df.drop() on the dataframe to drop multiple columns. We will use an array of column labels and select index column numbers for dropping.

Source Code:

import pandas as pd

stu_info = {'stu_name': ['Chris', 'Hemsworth', 'Hayden', 'Adam'],
	'stu_id': [178, 924, 1290, 6234],
	'Desgination': ['Developer', 'tester', 'Gamer', 'Q.a'],
	'stu_age': [17, 19, 21, 32]}


df = pd.DataFrame(stu_info)
df.drop(df.columns[[3,0,1]], axis = 1, inplace = True)
print("Updated dataframe after drop multile cols:")
print(df)

In the above code, we have created a dataframe and then use the drop() function on the Pandas DataFrame to remove multiple columns.

Once you will print ‘df’ then the output will display the updated dataframe that contains only a specific ‘designation’ column.

You can refer to the below Screenshot

Drop multiple columns in Pandas DataFrame
Drop multiple columns in Pandas DataFrame

Read Add row to Dataframe Python Pandas

How to drop multiple columns in Pandas

By using the Python Pandas df.pop() method we can perform this particular task and this function is used to remove a specific column.

Syntax:

Here is the Syntax of DataFrame.pop() method

DataFrame.pop(item)

Source Code:

import pandas as pd

new_val_lis = [('Elijah', 1829, 'China',   32) ,
            ('Potter',    3449, 'France' ,  17) ,
            ('James',    9234, 'Newzealand', 19) ,
            ('George',    13490, 'Germany' ,   21)]

df = pd.DataFrame( new_val_lis, 
                    columns=['Stu_name', 'Stu_id', 'Stu_city', 'Stu_age'])

df.pop(df.columns[-1])
df.pop(df.columns[-2])
print("Updated Dataframe:")
print(df)

Here is the Screenshot of the following given code

Drop multiple columns in Pandas DataFrame
Drop multiple columns in Pandas DataFrame

Drop duplicate columns in Pandas DataFrame

  • In this program, we will discuss how to drop duplicate columns in Pandas DataFrame.
  • In Python Pandas.drop_duplicate() method will help the user to remove duplicate columns or rows from the Pandas DataFrame.

Syntax:

Here is the Syntax of Pandas.DataFrame.duplicate() method

DataFrame.dropduplicates
                        (
                         subset=None,
                         keep='first',
                         inplace=False,
                         ignore_index=False
                        )
  • It consists of few parameters
    • Subset: This parameter indicates the list of column labels and by default it is set as None value.
    • keep: By default it takes as ‘first’ value that means drop the duplicate values except for the first element.

Source Code:

import pandas as pd
import numpy as np
new_val = np.random.randint(0,10, (4,3))


df = pd.DataFrame(np.hstack([new_val, new_val]), columns=['z', 'd', 'b', 'x', 'b', 'z'] )
print(df)
print("Updated dataframe:")
print(df.T.drop_duplicates().T)

Here is the implementation of the following given code

Drop duplicate columns in Pandas DataFrame
Drop duplicate columns in Pandas DataFrame

Remove first column in Pandas DataFrame

In this topic, you can use the drop() function, and also you can refer to the above topic Drop the first column in Pandas DataFrame.

Remove column names in Pandas DataFrame

  • Let us see how to remove a column name in Pandas DataFrame.
  • In this example, we have selected the ‘val2’ column name to remove from Pandas dataframe. To do this task we have to use the df.drop() method and this function will help you to drop specific column names from the dataframe.

Source Code:

import pandas as pd

new_dict = {
	'val1':['m', 'o', 'a', 'p', 'z'],
	'val2':['z', 'm', 'u', 'x', 'y'],
	'val3':['11', '55', '806', '22', '66'],
	'val4':['389', '32', '180', '378', '674'],
	'val5':['134', '809', '457', '293', '3939'] }

df = pd.DataFrame(new_dict)
d= df.drop(['val2'], axis = 1)
print(d)

Here is the implementation of the following given code

Remove column names in Pandas DataFrame
Remove column names in Pandas DataFrame

Drop column Pandas series

  • Here we can see how to drop the column in Pandas Series.
  • By using DataFrame.drop() method we can easily solve this problem and this method will drop elements of a series based on particular the index labels.

Syntax:

Here is the Syntax of Pandas.Series.drop() method

Series.drop
           (
            labels=None,
            axis=0,
            index=None,
            columns=None,
            Level=None,
            inplace=False,
            errors='raise'

Example:

import numpy as np
import pandas as pd

new_dat = pd.Series(data=np.arange(3), index=['China', 'Japan', 'Australia'])
b=new_dat.drop(labels=['Japan', 'Australia'])
print(b)

In the above code first, we have imported numpy and Pandas library and then create a Pandas series by using pd. series and assign a number of values by applying np.arange() function.

Here is the Output of the following given code

Drop column Pandas series
Drop column Pandas series

Drop one column in Pandas DataFrame

  • Here we can see how to drop one column in Pandas DataFrame.
  • By using the df.drop() method we can perform this particular task and in this example first, we have created a dictionary and contains key-value pair elements. Now use the df.drop() method and assign a specific column value.

Example:

import pandas as pd

new_dt = {
	'val3':['93', '921', '1889', '3765', '126'],
	'val4':['1021', '446', '9578', '129', '389'],
	'col2':['467', '13456', '489', '356', '221'],
	'col3':['Elijah', 'George', 'Micheal', 'oliva', '390']
}

df = pd.DataFrame(new_dt)

b= df.drop(['col2'], axis = 1)
print(b)

You can refer to the below Screenshot

Drop one column in Pandas DataFrame
Drop one column in Pandas DataFrame

Drop list of column in Pandas DataFrame

  • In the Program, we will discuss how to drop list of column in Pandas DataFrame.
  • In Python Pandas the iloc() method is used to select a specific cell of the Dataset and this method accepts only integer values and also we cannot pass boolean values as an index.

Example:

import pandas as pd

new_data = [('Stever',  782, 'Malayasia',   12) ,
            ('Roger',   230, 'Newzealand' ,  23) ,
            ('Geroge',  119, 'Australia',   36) ,
            ('Oliva',   534, 'Bangladesh' ,  19)]

df = pd.DataFrame(  new_data, 
                    columns=['Stu_name', 'Stu_id', 'Stu_city', 'Stu_age'])

new_val = df.iloc[: , 1:]
new_val = df.iloc[: , 2:]
print("Updated Dataframe after removing first col : ")
print(new_val)

In the above code, we have removed two specific columns that are ‘Stu_name’ and ‘Stu_id’. Once you will print ‘new_val’ then the output will display the updated dataframe.

Here is the execution of the following given code

Drop list of column in Pandas DataFrame
Drop list of columns in Pandas DataFrame

You may like the following Python tutorials:

In this tutorial, we have learned about Pandas Delete Column. Also, we have covered these topics.

  • Pandas Delete Column DataFrame
  • Pandas Delete Column by Name
  • Pandas Delete Column by Index
  • Pandas Delete Column if Exists
  • Pandas Delete Column by Condition
  • Pandas Delete Columns with NaN
  • Pandas Delete Column if all nan
  • Pandas Delete Column by Position
  • Pandas Delete Column with no Name
  • Pandas Delete Column Header
  • Pandas Delete Columns Except
  • Drop first column in Pandas DataFrame
  • Drop last column in Pandas DataFrame
  • Drop multiple columns in Pandas DataFrame
  • Drop duplicate columns in Pandas DataFrame
  • Remove first column in Pandas DataFrame
  • Remove column names in Pandas DataFrame
  • Drop column Pandas series
  • Drop one column in Pandas DataFrame
  • Drop list of column in Pandas DataFrame