0
0
PandasHow-ToBeginner · 3 min read

How to Drop a Column in pandas DataFrame Easily

To drop a column in pandas, use the drop() method with the column name and set axis=1. For example, df.drop('column_name', axis=1) removes the specified column from the DataFrame.
📐

Syntax

The basic syntax to drop a column in pandas is:

  • df.drop(labels, axis=1, inplace=False)

Here:

  • labels is the column name or list of column names to drop.
  • axis=1 tells pandas to drop columns (not rows).
  • inplace=False returns a new DataFrame without the column; inplace=True modifies the original DataFrame.
python
df.drop('column_name', axis=1)
df.drop(['col1', 'col2'], axis=1, inplace=True)
💻

Example

This example shows how to drop a single column and multiple columns from a pandas DataFrame.

python
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['NY', 'LA', 'Chicago']}
df = pd.DataFrame(data)

# Drop single column 'Age'
df_dropped = df.drop('Age', axis=1)

# Drop multiple columns 'Age' and 'City' inplace
df.drop(['Age', 'City'], axis=1, inplace=True)

print('DataFrame after dropping Age column (new DataFrame):')
print(df_dropped)

print('\nOriginal DataFrame after dropping Age and City columns inplace:')
print(df)
Output
DataFrame after dropping Age column (new DataFrame): Name City 0 Alice NY 1 Bob LA 2 Charlie Chicago Original DataFrame after dropping Age and City columns inplace: Name 0 Alice 1 Bob 2 Charlie
⚠️

Common Pitfalls

Common mistakes when dropping columns include:

  • Forgetting to set axis=1, which causes pandas to try dropping rows instead.
  • Not using inplace=True when you want to modify the original DataFrame, leading to no change.
  • Trying to drop a column that does not exist, which raises a KeyError.
python
import pandas as pd

data = {'A': [1, 2], 'B': [3, 4]}
df = pd.DataFrame(data)

# Wrong: axis missing, tries to drop rows
try:
    df.drop('A')
except Exception as e:
    print(f'Error: {e}')

# Wrong: inplace missing, original df unchanged
new_df = df.drop('A', axis=1)
print('Original df columns:', df.columns.tolist())
print('New df columns:', new_df.columns.tolist())

# Wrong: dropping non-existent column
try:
    df.drop('C', axis=1)
except Exception as e:
    print(f'Error: {e}')
Output
Error: "No axis named A for object type DataFrame" Original df columns: ['A', 'B'] New df columns: ['B'] Error: "['C'] not found in axis"
📊

Quick Reference

ParameterDescriptionExample
labelsColumn name or list of columns to drop'Age' or ['Age', 'City']
axis1 to drop columns, 0 to drop rowsaxis=1
inplaceModify original DataFrame if Trueinplace=True
errorsIgnore if column not found if set to 'ignore'errors='ignore'

Key Takeaways

Use df.drop('column_name', axis=1) to drop columns in pandas.
Set inplace=True to modify the original DataFrame directly.
Always specify axis=1 to indicate you want to drop columns, not rows.
Dropping a non-existent column raises a KeyError unless errors='ignore' is used.
You can drop multiple columns by passing a list of column names.