Select columns
Remove unwanted fields while preserving the source DataFrame.
Pandas · Module
DataFrame.drop removes selected labels from rows or columns. Use index= or columns= for readable, axis-free calls; it returns a new DataFrame unless inplace=True.
DataFrame.drop removes selected row-index or column labels. Pass labels with an axis, or use index and columns directly; by default it returns a new DataFrame, while inplace=True changes the object and returns None.
DataFrame.drop(labels=None, *, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')| Name | Type | Description |
|---|---|---|
labels | single label or list-like | Index or column labels to drop. |
axis | {0 or 'index', 1 or 'columns'}, default 0 | Select rows with 0 or 'index', or columns with 1 or 'columns'. |
index | single label or iterable of labels | Alternative to labels with axis=0. |
columns | single label or iterable of labels | Alternative to labels with axis=1. |
level | int or level name, optional | MultiIndex level from which labels are removed. |
inplace | bool, default False | Modify the DataFrame in place. |
errors | {‘ignore’, ‘raise’}, default ‘raise’ | Whether to raise for missing labels; 'ignore' suppresses those errors. |
DataFrame or None: the DataFrame with specified index or column labels removed, or None when inplace=True.
import pandas as pd
orders = pd.DataFrame({
"customer": ["A", "B", "C"],
"total": [12, 25, 18],
}, index=[101, 102, 103])
without_total = orders.drop(columns="total")
without_b = orders.drop(index=102)
print(without_total)
print(without_b)Use the explicit columns or index keyword to remove labels without relying on axis numbers.
Remove unwanted fields while preserving the source DataFrame.
Exclude records identified by their index labels.
Remove labels from one level of a hierarchical index.
Process partially varying schemas without failing on absent labels.
It removes specified labels from rows or columns. The axis argument selects index labels with 0 or 'index', and column labels with 1 or 'columns'.
Follow-up: How can the same operation be expressed without axis? Use index= for rows or columns= for columns; these are alternatives to labels combined with the corresponding axis.
The default is inplace=False, so the method returns a DataFrame with the requested labels removed and leaves the original unchanged. With inplace=True, it modifies the object and returns None.
Follow-up: When is inplace=True observable from the return value? The result is None, so callers must not assign it as though it were the modified DataFrame.
For a MultiIndex, level identifies the index level from which labels are removed. It can be an integer level position or a level name.
Follow-up: Why specify level? It disambiguates removal in a multi-level index and avoids treating the label as a match across every level.
DataFrame.popDataFrame.drop_duplicatesDataFrame.dropnaDataFrame.renameDataFrame.set_axis