Delete a column from a Pandas DataFrame
How can you remove an unwanted column from a Pandas DataFrame in Python?
In Pandas, deleting a column from a DataFrame is a very common task, especially when you’re cleaning data or preparing it for analysis. Sometimes, datasets come with extra columns that are not needed, and removing them makes your DataFrame more organized and efficient. Luckily, Pandas provides simple ways to do this.
Here are the most common methods:
Using drop()
You can use the drop() method and specify the column name. For example:
df = df.drop('column_name', axis=1)
- axis=1 tells Pandas to drop a column (use axis=0 for rows).
- If you don’t want to create a new DataFrame, you can add inplace=True.
Dropping multiple columns
If you want to remove more than one column, just pass a list:
df = df.drop(['col1', 'col2'], axis=1)
Using del keyword
You can directly delete a column like this:
del df['column_name']
This permanently removes the column from the DataFrame.
Using pop()
The pop() method removes a column and also returns it, which is useful if you need the data separately:
removed_col = df.pop('column_name')
In short, if you want flexibility and cleaner code, drop() is the most commonly used method. For quick deletions, del or pop can be very handy.