Here is how to delete a column from a Pandas DataFrame in Python.


import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Delete column 'B'
df.drop(columns='B', inplace=True) 

Output

 


Explanation

To delete a column from a Pandas DataFrame in Python, you can use the drop() function. You can specify the column to be dropped by passing its name as a string to the columns parameter. To delete the column in place and not to return a new DataFrame, you can set the inplace parameter to True.

If you want to drop multiple columns at once you can pass a list of column names to the columns parameter:

# Delete columns 'A' and 'C'
df.drop(columns=['A', 'C'], inplace=True)
Please note that inplace=True will change the DataFrame in place and returns None, the original DataFrame will be modified. If you want to keep the original DataFrame, you can assign the result to a new variable.



Related HowTos
Add a new column to an Excel file
Select multiple columns in a Pandas dataframe
Rename column names with Pandas
Select rows from a DataFrame based on column values with Pandas
Iterate over rows in a DataFrame for Pandas
Create a pandas DataFrame from a dictionary
Create a pandas DataFrame from a list
Delete a column from a pandas dataframe
Access a column of a pandas dataframe
Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.