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