Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to delete a column from Pandas DataFrame
To delete a column from a Pandas DataFrame, you have several methods available: del statement, drop() method, and pop() method. Each approach has its own use cases and advantages.
Using del Statement
The del statement permanently removes a column from the DataFrame ?
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
"Units": [100, 150, 110, 80, 110, 90],
"Price": [50000, 60000, 45000, 40000, 80000, 70000]
})
print("Original DataFrame:")
print(dataFrame)
# Delete a column using del
del dataFrame['Price']
print("\nDataFrame after deleting 'Price' column:")
print(dataFrame)
Original DataFrame:
Car Units Price
0 BMW 100 50000
1 Lexus 150 60000
2 Audi 110 45000
3 Mustang 80 40000
4 Bentley 110 80000
5 Jaguar 90 70000
DataFrame after deleting 'Price' column:
Car Units
0 BMW 100
1 Lexus 150
2 Audi 110
3 Mustang 80
4 Bentley 110
5 Jaguar 90
Using drop() Method
The drop() method is more flexible and can delete multiple columns. By default, it returns a new DataFrame ?
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Audi', 'Mustang'],
"Units": [100, 150, 110, 80],
"Price": [50000, 60000, 45000, 40000],
"Year": [2020, 2021, 2019, 2018]
})
print("Original DataFrame:")
print(dataFrame)
# Drop single column (returns new DataFrame)
new_df = dataFrame.drop('Price', axis=1)
print("\nAfter dropping 'Price' column:")
print(new_df)
# Drop multiple columns
new_df2 = dataFrame.drop(['Price', 'Year'], axis=1)
print("\nAfter dropping multiple columns:")
print(new_df2)
Original DataFrame:
Car Units Price Year
0 BMW 100 50000 2020
1 Lexus 150 60000 2021
2 Audi 110 45000 2019
3 Mustang 80 40000 2018
After dropping 'Price' column:
Car Units Year
0 BMW 100 2020
1 Lexus 150 2021
2 Audi 110 2019
3 Mustang 80 2018
After dropping multiple columns:
Car Units
0 BMW 100
1 Lexus 150
2 Audi 110
3 Mustang 80
Using pop() Method
The pop() method removes the column and returns it as a Series ?
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Audi'],
"Units": [100, 150, 110],
"Price": [50000, 60000, 45000]
})
print("Original DataFrame:")
print(dataFrame)
# Pop a column (removes and returns it)
popped_column = dataFrame.pop('Price')
print("\nDataFrame after popping 'Price' column:")
print(dataFrame)
print("\nPopped column:")
print(popped_column)
Original DataFrame:
Car Units Price
0 BMW 100 50000
1 Lexus 150 60000
2 Audi 110 45000
DataFrame after popping 'Price' column:
Car Units
0 BMW 100
1 Lexus 150
2 Audi 110
Popped column:
0 50000
1 60000
2 45000
Name: Price, dtype: int64
Comparison
| Method | Modifies Original | Returns Value | Multiple Columns |
|---|---|---|---|
del |
Yes | None | No |
drop() |
No (by default) | New DataFrame | Yes |
pop() |
Yes | Removed Series | No |
Conclusion
Use del for simple column deletion, drop() for flexible operations with multiple columns, and pop() when you need the removed column data. The drop() method is most commonly used as it's safer and more versatile.
Advertisements
