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
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

Learn Flask development and learn to build cool apps with our premium Python course on Udemy.


Subscribe to be the first to know when a new Python course is released.