drop() (Pandas)

df.drop() removes specified rows or columns from a DataFrame by their labels. Use columns=... to delete columns and index=... (or labels with axis) to delete rows.

Pandas

The clearest form names the axis explicitly: df.drop(columns=['a', 'b']) removes those columns, and df.drop(index=[0, 3]) removes those rows by label. The older style uses df.drop(labels, axis=1) for columns and axis=0 for rows.

drop matches on labels, so on a default integer index df.drop(index=2) removes the row labeled 2, which may not be the third row if the index was reordered. To drop by position, select with iloc instead. Pass errors='ignore' to skip labels that aren't present rather than raising.

Like most pandas methods it returns a new frame; reassign it or use inplace=True. To delete by condition rather than by label, filter with a boolean mask instead of drop.

Syntax

df.drop(labels=None, axis=0, index=None, columns=None)

Examples

Python

import pandas as pd
df = pd.DataFrame({"a": [1], "b": [2]})
print(df.drop(columns="b"))

Output:

   a
0  1

Learn it in a lesson

See also

Back to the Dictionary