How can a column of a dataframe be deleted in Python?

A DataFrame is a two-dimensional data structure where data is stored in tabular format with rows and columns. It can be visualized as an SQL table or Excel sheet. There are several methods to delete columns from a DataFrame in Python pandas.

Using the del Operator

The del operator permanently removes a column from the DataFrame ?

import pandas as pd

my_data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'London', 'Paris'],
    'Salary': [50000, 60000, 70000]
}

df = pd.DataFrame(my_data)
print("Original DataFrame:")
print(df)

print("\nDeleting the 'City' column using del operator:")
del df['City']
print(df)
Original DataFrame:
      Name  Age      City  Salary
0    Alice   25  New York   50000
1      Bob   30    London   60000
2  Charlie   35     Paris   70000

Deleting the 'City' column using del operator:
      Name  Age  Salary
0    Alice   25   50000
1      Bob   30   60000
2  Charlie   35   70000

Using drop() Method

The drop() method provides more flexibility and can return a new DataFrame without modifying the original ?

import pandas as pd

data = {
    'Product': ['A', 'B', 'C'],
    'Price': [100, 150, 200],
    'Stock': [50, 30, 80],
    'Category': ['Electronics', 'Clothing', 'Books']
}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# Method 1: Create new DataFrame (original unchanged)
df_new = df.drop('Stock', axis=1)
print("\nUsing drop() - original unchanged:")
print(df_new)

# Method 2: Modify original DataFrame
df.drop('Category', axis=1, inplace=True)
print("\nUsing drop() with inplace=True:")
print(df)
Original DataFrame:
  Product  Price  Stock    Category
0       A    100     50  Electronics
1       B    150     30     Clothing
2       C    200     80       Books

Using drop() - original unchanged:
  Product  Price    Category
0       A    100  Electronics
1       B    150     Clothing
2       C    200       Books

Using drop() with inplace=True:
  Product  Price  Stock
0       A    100     50
1       B    150     30
2       C    200     80

Deleting Multiple Columns

You can delete multiple columns at once using the drop() method ?

import pandas as pd

data = {
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9],
    'D': [10, 11, 12],
    'E': [13, 14, 15]
}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# Delete multiple columns
df_modified = df.drop(['B', 'D', 'E'], axis=1)
print("\nAfter deleting columns B, D, and E:")
print(df_modified)
Original DataFrame:
   A  B  C   D   E
0  1  4  7  10  13
1  2  5  8  11  14
2  3  6  9  12  15

After deleting columns B, D, and E:
   A  C
0  1  7
1  2  8
2  3  9

Comparison of Methods

Method Modifies Original Multiple Columns Best For
del df['col'] Yes No Single column deletion
df.drop() No (unless inplace=True) Yes Flexible deletion with options
df.drop(inplace=True) Yes Yes Permanent multiple deletions

Conclusion

Use del for simple single column deletion. Use drop() method for more control, especially when deleting multiple columns or when you want to preserve the original DataFrame.

Updated on: 2026-03-25T13:13:02+05:30

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements