Delete Pandas DataFrame Column

  1. del Method to Delete DataFrame Column
  2. df.drop Method to Delete DataFrame Columns
  3. df.pop Method to Delete DataFrame Column in Pandas

This article introduces different methods to delete DataFrame columns in Pandas,

  • del method
  • df.drop method
  • df.pop method

We will use the same DataFrame to demonstrate different methods in this article.

>>> from pandas import DataFrame
>>> df = DataFrame.from_items(
    [('Alfa', [1, 2, 3]), 
     ('Bravo', [4, 5, 6]), 
     ('Charlie', [7,8, 9])], 
    orient='index', 
    columns=['A', 'B', 'C'])
>>> df
         A  B  C
Alfa     1  2  3
Bravo    4  5  6
Charlie  7  8  9

del Method to Delete DataFrame Column

>>> df
         A  B  C
Alfa     1  2  3
Bravo    4  5  6
Charlie  7  8  9
>>> del df['A']
>>> df
         B  C
Alfa     2  3
Bravo    5  6
Charlie  8  9

del df['col_name'] deletes the DataFrame column that has the names as col_name.

The limitation of this del method is that it could only delete one column at one time.

df.drop Method to Delete DataFrame Columns

drop(self, labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

drop method drops, or in other words removes/deletes the specified labels from rows or columns.

labels could be a single label or list-like index or column labels to drop.

axis specifies whether the labels are dropped from the index/row (0 or index) or column (1 or columns) .

index, columns is the alternative to specifying axis. drop(labels, axis=0) is equal to drop(index=labels), meanwhile drop(labels, axis=1) is equal to drop(column=labels).

inplace specifies the DataFrame is modified in place if inplace = True, otherwise, it returns the new DataFrame with the original DataFrame unmodified.

>>> df
         A  B  C
Alfa     1  2  3
Bravo    4  5  6
Charlie  7  8  9
>>> df.drop(["B", "C"], axis=1)
         A
Alfa     1
Bravo    4
Charlie  7

## or equally
>>> df.drop(columns=["B", "C"])
         A
Alfa     1
Bravo    4
Charlie  7

## or drop the columns in place
>>> df.drop(columns=["B", "C"],inplace=True)
>>> df
         A
Alfa     1
Bravo    4
Charlie  7

df.pop Method to Delete DataFrame Column in Pandas

df.pop(item)

DataFrame pop method returns the item and drops it from the DataFrame.

>>> df
         A  B  C
Alfa     1  2  3
Bravo    4  5  6
Charlie  7  8  9
>>> df.pop("A")
Alfa       1
Bravo      4
Charlie    7
Name: A, dtype: int64
>>> df
         B  C
Alfa     2  3
Bravo    5  6
Charlie  8  9        

The DataFrame data is modified in place as shown above.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Pandas DataFrame

  • Get Index of All Rows Whose Particular Column Satisfies Given Condition in Pandas
  • Sort Pandas DataFrame by One Column's Values
  • Related Article - Pandas DataFrame Column

  • Count the NaN Occurrences in a Column in Pandas Dataframe
  • Convert Object to Float in Pandas