To delete a column in a pandas DataFrame in Python, you can use several methods. Below are three common ways to do it using the pandas library.
Let consider the following dataframe as an example:
import pandas as pd
# Sample DataFrame
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
})
Option 1: using the drop
method
The drop
method is versatile and can be used to drop both rows and columns. To delete a column, you'll need to specify the column name and set the axis
parameter to 1
(or 'columns'
). You also need to decide if you want to modify the existing DataFrame or return a new DataFrame with the column deleted.
Here's how to use it:
# Dropping column 'B'
df = df.drop('B', axis=1)
# or alternatively for column labels:
# df.drop(columns='B', inplace=True)
print(df)
Option 2: Using del
statement
If you're sure you no longer need the column and you want to modify the DataFrame in place, you can use the del
statement. This is a straightforward and efficient way to remove a column:
# Deleting column 'B'
del df['B']
print(df)
Option 3: Using pop
method
The pop
method not only deletes the column but also returns it as a Series, which can be useful if you want to use the column for further operations after its deletion from the DataFrame:
# Popping column 'B'
popped_column = df.pop('B')
print("Deleted Column:\n", popped_column)
print("\nRemaining DataFrame:\n", df)
Choose the method that best suits your needs based on whether you want to modify the DataFrame in place, whether you need the deleted data, or whether you prefer a more functional approach.