How to Use Pandas Drop Columns to Delete DataFrame Columns
Use DataFrame.drop() with the columns parameter to remove one or more columns, and set inplace=True to modify the original DataFrame instead of returning a copy.
The pandas-dev/pandas library provides the drop method as the standard way to delete columns from a DataFrame. According to the source code in pandas/core/frame.py, this method validates your column labels and delegates to the underlying indexing machinery to perform the removal.
Understanding the DataFrame.drop() Method Signature
The drop method is implemented in pandas/core/frame.py and inherits core logic from pandas/core/generic.py. The full signature gives you fine-grained control over what to remove and how to handle errors:
DataFrame.drop(labels=None, axis=0, index=None, columns=None,
level=None, inplace=False, errors='raise')
Key parameters for deleting columns:
columns– A single label or list-like of column names to drop. This is the preferred way to specify columns rather than usingaxis=1.inplace– IfTrue, the method modifies the original DataFrame and returnsNone. IfFalse(default), it returns a new DataFrame with the columns removed.errors– Set to'raise'(default) to throw aKeyErrorif a column doesn't exist, or'ignore'to skip missing labels silently.
How to Drop a Single Column in Pandas
To remove one column, pass the column name as a string to the columns parameter. This returns a new DataFrame without modifying the original:
import pandas as pd
df = pd.DataFrame({
"A": [1, 2, 3],
"B": [4, 5, 6],
"C": [7, 8, 9],
})
# Drop column "B"
df_new = df.drop(columns="B")
print(df_new)
How to Drop Multiple Columns at Once
Pass a list of column names to remove several columns in a single operation. This is more efficient than calling drop multiple times:
# Drop columns "A" and "C" simultaneously
df_reduced = df.drop(columns=["A", "C"])
print(df_reduced)
In-Place vs. Copy: Modifying DataFrames Directly
By default, DataFrame.drop() follows pandas' functional paradigm and returns a new object. To modify the existing DataFrame without assignment, use inplace=True:
# Modify df directly; returns None
df.drop(columns="A", inplace=True)
print(df)
Warning: When inplace=True, the method returns None. Do not assign the result back to a variable or you will overwrite your DataFrame with None.
Handling Missing Columns Safely
If your code might attempt to drop columns that don't exist (for example, in dynamic data pipelines), set errors='ignore' to prevent KeyError exceptions:
# Safely drop "Z" even if it doesn't exist
df_safe = df.drop(columns="Z", errors="ignore")
print(df_safe)
Summary
- Use
DataFrame.drop(columns="col_name")to delete columns from a pandas DataFrame. - The method is implemented in
pandas/core/frame.pyand delegates topandas/core/generic.py. - Pass a list to
columnsto drop multiple columns efficiently:drop(columns=["A", "B"]). - Set
inplace=Trueto modify the original DataFrame; otherwise the method returns a new copy. - Use
errors="ignore"to safely handle attempts to drop non-existent columns.
Frequently Asked Questions
What is the difference between del df['col'] and df.drop()?
The del statement removes a single column by modifying the DataFrame in-place immediately and does not return anything. df.drop() is a method that can remove multiple columns, offers the inplace parameter for optional modification, and returns the resulting DataFrame (or None if inplace=True). Use del for quick single-column deletion, and drop() for flexible, programmatic column management.
Does drop() modify the original DataFrame?
No, drop() returns a new DataFrame by default and leaves the original unchanged. This follows pandas' copy-on-write philosophy to prevent accidental data loss. To modify the original DataFrame directly, you must explicitly set the inplace=True parameter, at which point the method returns None instead of a DataFrame.
How do I drop columns by index position instead of name?
To drop columns by their integer position, use DataFrame.columns indexing combined with drop(). For example: df.drop(columns=df.columns[[0, 2]]) drops the first and third columns. Alternatively, you can use df.iloc to select the columns you want to keep and reassign: df = df.iloc[:, [1, 3]]. The drop() method itself requires label names, not positions.
What happens if I try to drop a column that doesn't exist?
By default, pandas raises a KeyError if you attempt to drop a column that is not present in the DataFrame. This behavior is controlled by the errors parameter, which defaults to 'raise'. To suppress the error and skip the missing column silently, pass errors='ignore' to the drop() method. This is particularly useful when writing defensive data cleaning scripts where column existence is uncertain.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s https://instagit.com/install.md Maintain an open-source project? Get it listed too →