

How to delete a column from a Pandas DataFrame?
source link: https://thispointer.com/how-to-delete-a-column-from-a-pandas-dataframe/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

- drop() method
- del Command
- 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
Recommend
-
121
Count number of Zeros in Pandas Dataframe Column This article will discuss how to count the number of zeros in a single or all column of a Pandas Dataframe. Let’s first create a Dataframe from a list of...
-
10
Pandas | Count non-zero values in Dataframe Column This article will discuss how to count the number of non-zero values in one or more Dataframe columns in Pandas. Let’s first create a Dataframe from a...
-
14
Pandas – Count True Values in a Dataframe Column In this article, we will discuss different ways to count True values in a Dataframe Column. First of all, we will create a Dataframe from a list of tuples...
-
7
How do I write Spark Optimization to recalculate DataFrame when the table column contains more values than a threshold? advertisements ...
-
16
This article will discuss how to rename column names in Pandas DataFrame. Table of Contents A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using pandas.D...
-
12
How to Drop Index Column of a Pandas DataFrame – thisPointer In this article, we will discuss about the different ways to drop the Index Column of a Pandas DataFrame. A DataFrame is a data structure that stores the data in rows...
-
13
Check if a Column exists in Pandas DataFrame In this article, we will discuss how to check if a column or multiple columns exist in a Pandas DataFrame or not. Suppose we have a DataFrame, ...
-
12
Convert the Column type from String to Datetime format in Pandas DataframeConvert the Column type from String to Datetime format in Pandas Dataframe10 Views30/05/2022...
-
6
Add Column to Pandas DataFrame with constant value In this article, we will learn different ways to add a column in a DataFrame with a constant value. Table Of Contents Sup...
-
12
January 13, 2023 /
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK