How to delete a column from a Pandas DataFrame?

In this article, we will discuss different ways to delete a column from a DataFrame in Pandas.

Table Of Contents

What is Pandas DataFrame?

Pandas DataFrame is a labelled two dimensional data structure with rows and columns. It is a Two-dimensional, size-mutable, potentially heterogeneous tabular data structure. we can perform arithmetic operations align on both row and column labels of DataFrame.

The Pandas DataFrame contains three elements,
1. Data
2. Rows
3. Columns

Syntax of Pandas DataFrame

pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=None)
  • data : data can be of type like ndarray, series, map, lists, dict, constants and also another DataFrame
  • index : Index to use for resulting frame. Will default to RangeIndex if no indexing information part of input data and no index provided
  • columns : Index or array-like, Column labels
  • dtype : data – type, default None Data type to force. Only a single dtype is allowed.
  • copy : bool or Non value, default None, Copy data from inputs. For dict data, the default of None behaves like copy=True. For DataFrame or 2d ndarray input, the default of None behaves like copy=False.

There are three different ways to delete column from data Frame,

Advertisements
  1. drop() method
  2. del Command
  3. Pop() command

Difference between drop() method and del command

  • The drop() method can operate on multiple items at a time whereas del operates only on one at a time.
  • The drop() can operate in-place or return a resulting set where as del is an in-place operation only.
  • The drop() method can be applied on both columns and rows whereas del can be used applied on column only.

Delete a DataFrame Column using drop() method

Details about drop() Method of pandas

  • The drop() method is used to remove specified labelled row or column.
  • The drop() method removes the column by specified corresponding axis axis=’columns’, or by specifying directly index or column names.
  • The drop() method removes the row by specified corresponding axis axis=’index’, or by specifying directly index

Syntax of Drop() method

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

Let’s see some examples of deleting column using drop() method.

Droping column from DataFrame using column name

import pandas as pd

# create a DataFrame with Three columns
data = {
  "Rollno": [1,2,3],
  "name": ["reema", "rekha", "jaya"],
  "city": ["surat", "Vadodara", "vapi"]
}

df = pd.DataFrame(data)
print(df)

# Drop column 'city' from DataFrame
newdf = df.drop("city", axis='columns')

print(newdf)

Output

Rollno   name      city
0       1  reema     surat
1       2  rekha  Vadodara
2       3   jaya      vapi

Rollno   name
0       1  reema
1       2  rekha
2       3   jaya

It deleted column “city” from the DataFrame.

Drop columns from DataFrame using column index

import pandas as pd

# create a DataFrame with Three columns
data = {
  "Rollno": [1,2,3],
  "name": ["reema", "rekha", "jaya"],
  "city": ["surat", "Vadodara", "vapi"]
}

df = pd.DataFrame(data)
print(df)

# Delete column at index position 1 from DataFrame
newdf=df.drop(df.iloc[:, 1::2], axis = 1)

print(newdf)

Output

Rollno   name      city
0       1  reema     surat
1       2  rekha  Vadodara
2       3   jaya      vapi

Rollno      city
0       1     surat
1       2  Vadodara
2       3      vapi

It deleted the column at index position 1 i.e. the column “name” from the DataFrame.

Dropping more than one columns from Data Frame using column names

import pandas as pd

# create a DataFrame with Three columns
data = {
  "Rollno": [1,2,3],
  "name": ["reema", "rekha", "jaya"],
  "city": ["surat", "Vadodara", "vapi"]
}

df = pd.DataFrame(data)
print(df)

# Delete columns "name" and "city" from DataFrame
newdf=df.drop(df.loc[:, ['name', 'city']], axis = 1)

print(newdf)

Output

Rollno   name      city
0       1  reema     surat
1       2  rekha  Vadodara
2       3   jaya      vapi

Rollno
0       1
1       2
2       3

It deleted columns “name” and “city” from the DataFrame

Delete Columns from DataFrame using del keyword

  • The del keyword in python is used to delete any object, and this object can be a list , variable, column, row and dictionary.
  • The del keyword is also used to delete item at a given index from array, list or directory, It can also be used to remove slices from a list.

Syntax of del command

del  object_name

Let’s see some examples of deleting column from DataFrame using Del command,

Using del command to delete column by name

import pandas as pd

# create a dictionary with five fields each
data = {
  "Rollno": [1,2,3],
  "name": ["reema", "rekha", "jaya"],
  "city": ["surat", "Vadodara", "vapi"]
}

df = pd.DataFrame(data)
print(df)

# Delete colum "name" from DataFrame
del df['name']

print(df)

Output

Rollno   name      city
0       1  reema     surat
1       2  rekha  Vadodara
2       3   jaya      vapi

Rollno      city
0       1     surat
1       2  Vadodara
2       3      vapi

It deleted the “name” column from the DataFrame.

Delete Columns from Pandas DataFrame using pop()

The pandas.dataframe.pop() method is used to remove or delete a column from a DataFrame by just specifying the name of the column as an argument.

Syntax of pandas pop() method

Dataframe.pop(‘column name’)

Let’s see some examples pf deleting columns using pandas pop() method.

Using pop() method to remove a column by name

import pandas as pd

# create a dictionary with five fields each
data = {
  "Rollno": [1,2,3],
  "name": ["reema", "rekha", "jaya"],
  "city": ["surat", "Vadodara", "vapi"]
}

df = pd.DataFrame(data)
print(df)

# Drop column 'name' from DataFrame
df.pop('name')

print(df)

Output

Rollno   name      city
0       1  reema     surat
1       2  rekha  Vadodara
2       3   jaya      vapi

Rollno      city
0       1     surat
1       2  Vadodara
2       3      vapi

It deleted the “name” column from the DataFrame.

Summary

In this article, we have discussed what is dataframe in pandas, syntax of dataframe, how to create dataframe, what are the ways to remove columns from datafame in pandas, and also explained each methods with examples.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top