How to delete a column from a Pandas DataFrame ?

To delete a column from a Pandas DataFrame, you can use several methods. Below are the most common and efficient approaches:

1. Using drop() Method

The drop() method is the most flexible way to remove columns. It returns a new DataFrame by default (does not modify the original unless specified).

Syntax:

df = df.drop(columns=['column_name'])  # Single column
df = df.drop(columns=['col1', 'col2']) # Multiple columns

Example:

import pandas as pd

# Sample DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)

# Delete column 'B'
df = df.drop(columns=['B'])
print(df)

Output:

   A  C
0  1  7
1  2  8
2  3  9

Modify Original DataFrame:

df.drop(columns=['B'], inplace=True)  # Directly modifies `df`

2. Using del Keyword

The del keyword deletes a column in-place (modifies the original DataFrame).

Syntax:

del df['column_name']

Example:

del df['B']
print(df)

Output:

   A  C
0  1  7
1  2  8
2  3  9

3. Using pop() Method

The pop() method removes a column in-place and returns the deleted column as a Series. Useful if you need the removed data.

Syntax:

removed_column = df.pop('column_name')

Example:

removed = df.pop('B')
print("Removed Column:\n", removed)
print("\nUpdated DataFrame:\n", df)

Output:

Removed Column:
0    4
1    5
2    6
Name: B, dtype: int64

Updated DataFrame:
   A  C
0  1  7
1  2  8
2  3  9

4. Column Selection (Keep Desired Columns)

Instead of deleting, you can select the columns you want to keep:

Example:

# Keep columns 'A' and 'C'
df = df[['A', 'C']]
print(df)

Output:

   A  C
0  1  7
1  2  8
2  3  9

Key Notes

  1. Axis Parameter:
  • Use axis=1 (or columns=['col']) to specify columns.
  • Example: df.drop('B', axis=1, inplace=True).
  1. Handling Errors:
  • Use errors='ignore' to avoid errors if the column does not exist:
    python df.drop(columns=['D'], errors='ignore', inplace=True)
  1. Performance:
  • del and pop() are faster for single-column deletion (in-place).
  • drop() is better for multiple columns or chaining operations.

Summary of Methods

MethodModifies Original?Returns Deleted Column?Use Case
df.drop(columns=['col'])No (unless inplace=True)NoFlexible, multiple columns
del df['col']YesNoQuick in-place deletion
df.pop('col')YesYesRetrieve and delete column
Column SelectionYesNoKeep specific columns

Example Workflow

# Create DataFrame
df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30], 'City': ['NYC', 'LA']})

# Delete 'City' using drop()
df = df.drop(columns=['City'])

# Delete 'Age' using del
del df['Age']

# Result
print(df)

Output:

    Name
0  Alice
1    Bob

Choose the method that best fits your workflow!

Leave a Reply

Your email address will not be published. Required fields are marked *