To delete a column from a Pandas DataFrame, you can use the drop() method with the axis parameter set to 1.
Here's an example:
import pandas as pd
# create a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Country': ['USA', 'Canada', 'UK']}
df = pd.DataFrame(data)
# drop the 'Country' column
df = df.drop('Country', axis=1)
# print the updated DataFrame
print(df)
This will output:
Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
0 Alice 25
1 Bob 30
2 Charlie 35
Note that the drop() method returns a new DataFrame with the specified column(s) dropped. To modify the original DataFrame in place, you can set the inplace parameter to True:
df.drop('Country', axis=1, inplace=True)
Comments (0)