How to drop columns in pandas?

Published on Aug. 22, 2023, 12:18 p.m.

To drop columns in a pandas DataFrame, you can use the drop() method. Here is an example:

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]})

# Drop the col2 column
df = df.drop(['col2'], axis=1)

# Print the new DataFrame
print(df)

In this code, we create a pandas DataFrame df with three columns. We then call the drop() method with the argument ['col2'] and axis=1 to remove the col2 column, and assign the result to df. The resulting DataFrame has only the col1 and col3 columns.

If you want to drop multiple columns, you can pass a list of column names to the drop() method:

# Drop multiple columns
df = df.drop(['col2', 'col3'], axis=1)

# Print the new DataFrame
print(df)

In this code, we pass ['col2', 'col3'] to the drop() method to remove both columns, and assign the result to df.

Note that the drop() method returns a new DataFrame with the specified columns removed, but does not modify the original DataFrame. If you want to modify the original DataFrame, you can pass the argument inplace=True:

# Drop the col2 column and modify the original DataFrame
df.drop(['col2'], axis=1, inplace=True)

# Print the new DataFrame
print(df)

This will remove the col2 column from the original DataFrame df and modify it in place.

Tags: