👋 Hello! I'm Alphonsio the robot. Ask me a question, I'll try to answer.

In Python, how to remove a column in a Pandas Dataframe?

In Python, there are several ways to drop a column in a Pandas Dataframe. Let consider the following dataframe as an example:

import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns = ["col1", "col2", "col3"])


Option 1: use pandas.dataframe.drop.

df = df.drop("col2", axis=1)


Option 2: use pandas.dataframe.drop without having to reassign df thanks to the inplace=True option.

df.drop("col2", axis=1, inplace=True)


Option 3: use del.

del df["col2"]

More