How to Delete Column(s) of Pandas DataFrame?

Python supports various tools and packages that are used to manipulate data. Data analysis or manipulation becomes fast, accurate, and easy using the “pandas” package of Python. A DataFrame is a 2-dimensional data structure used to organize the data similar to a table having rows and columns, just like a spreadsheet. To insert DataFrame various methods like the “insert()” method, append() method, etc., are used in Python.

To delete column(s) of pandas DataFrame, various methods, such as drop(), pop(), etc., are used in Python. This write-up will give you knowledge of how to delete column(s) of pandas DataFrame in Python. The following aspects are covered in this guide:

So, let’s get started!

Method 1: Using drop() Function

The “drop()” function is used to remove the specific column from the input pandas data frame. The syntax of the drop() function is shown below:

dataframe.drop(labels, axis, index, columns, level, inplace, errors)

In the above syntax:

  • The parameter “labels” takes the value of an index or column label that will be dropped.
  • The “axis” parameter takes the value “0” or “1”. Specify 0 to drop rows, while specifying 1 will drop the columns.
  • The “index” parameter is an alternative to specifying the axis ( labels, axis=0 is similar to index=labels).
  • The “columns” parameter is an alternative to specifying the axis ( labels, axis=0 is similar to index=labels).
  • The “level” parameter is used in multi-index data frames where it tells the level of the specific labels.
  • The “inplace” parameter, if true, will make changes in the original data frame otherwise, return a copy where the column or row removal is done.
  • The parameter “errors” specifies whether the errors will be ignored.

Example 1: Delete Single Column

Let’s understand the working of the “drop()” function with the help of the following example code:

Code:

import pandas as pd

dict_value = {'Names': ['John', 'Lily', 'David'],
    'Sex': ['Male', 'Female', 'Male'],'Age': [14, 16, 17],
    'Weight': [48, 48, 42]}

#using pd.dataframe() to create dataframe
df_value = pd.DataFrame(dict_value)
print('DataFrame Created: \n', df_value)

#delete column of created dataframe
df_value = df_value.drop(['Weight'], axis=1)
print('\n\nDataFrame Updated: \n', df_value)

In the above code:

  • The “pandas” library is imported as “pd” at the start of the code.
  • The dictionary is created, and the “pd.DataFrame()” function is used to create the data frame.
  • The pd.DataFrame() accepts a dictionary as an argument, and it will convert the given dictionary into a data frame.
  • The “dataframe.drop()” function takes the column and axis value “1” as an argument and returns the new data frame by removing the given columns. In the above code snippet, the drop() function is used to drop the “Weight” column of the given data frame.

Output:

The above output verified that the column “weight” had been removed from the data frame.

Example 2: Delete Multiple Columns

Let’s understand how we can delete multiple columns of the data frame using the “drop()” function with the help of the following example code:

Code:

import pandas as pd

dict_value = {'Names': ['John', 'Lily', 'David'],
    'Sex': ['Male', 'Female', 'Male'],'Age': [14, 16, 17],
    'Weight': [48, 48, 42]}

#using pd.dataframe() to create dataframe
df_value = pd.DataFrame(dict_value)
print('DataFrame Created: \n', df_value)

#delete column of created dataframe
df_value = df_value.drop(['Weight','Age'], axis=1)
print('\n\nDataFrame Updated: \n', df_value)

In the above code:

  • The code working is the same as the previous code example.
  • The only difference is that we passed columns named “Weight” and “Age” inside the “dataframe.drop()” function separated by a comma and enclosed in quotation marks.

Output:

The above output verified that the column “Weight” and “Age” had been removed from the given data frame.

Method 2: Using pop() Function

The “pop()” function deletes the particular column from the given data frame. The “pop()” function syntax is shown below:

dataframe.pop(label)

In the above syntax, the parameter “label” represents the label/name of the column that needs to be removed.

Example 1: Delete Column using pop() Function

Let’s see an example code given below in which the “pop()” function is used to remove a specific column from the given data frame.

Code:

import pandas as pd

dict_value = {'Names': ['John', 'Lily', 'David'],
    'Sex': ['Male', 'Female', 'Male'],'Age': [14, 16, 17],
    'Weight': [48, 48, 42]}

#using pd.dataframe() to create dataframe
df_value = pd.DataFrame(dict_value)
print('DataFrame Created: \n', df_value)

#delete column of created dataframe
df_value.pop('Names')
print('\n\nDataFrame Updated: \n', df_value)

In the above code:

  • The panda’s library is imported, and the dictionary is created in the program.
  • The “pd.Dataframe()” is used to create the data frame.
  • The “dataframe.pop()” accepts the column label as an argument and returns the data frame by removing the “Names” column.

Output:

The above output shows that the “Names” column has been removed from the data frame.

Method 3: Using del Keyword

The del keyword in Python deletes the specific column from the given data frame.

Code:

import pandas as pd

dict_value = {'Names': ['John', 'Lily', 'David'],
    'Sex': ['Male', 'Female', 'Male'],'Age': [14, 16, 17],
    'Weight': [48, 48, 42]}

#using pd.dataframe() to create dataframe
df_value = pd.DataFrame(dict_value)
print('DataFrame Created: \n', df_value)

#delete column of dataframe
del df_value['Age']
print('\n\nDataFrame Updated: \n', df_value)

In the above code:

  • The “pandas” library is imported, and the data frame is created using the “pd.DataFrame()” function.
  • The “del” keyword is used to delete the specific column of the data frame. In the above code, the del keyword is used along with the name of the data frame to delete the “Age” column of the given data frame.

Output:

The column label “Age” is successfully removed from the input data frame.

That’s it from this tutorial!

Conclusion

To delete column(s) of pandas DataFrame, the “drop()” function, “pop()” function, and the “del” keyword is used in Python. The “drop()” function is used to delete single and multiple columns of a pandas data frame. The “pop()” function takes the specified column label as an argument and deletes that specific column. The del keyword is also used to delete the given data frame column by taking the column’s label as a parameter value. This Python write-up demonstrated all the possible methods to delete column(s) of pandas DataFrame.