Syntax:
del(df['column_name'])
Note: Python maps this method to an internal method of DataFrame (df.__delitem__('column name')) which is responsible for the deletion of the column.
To work with Python Pandas, we need to import the pandas library. Below is the syntax,
import pandas as pd
Python code to delete a column from a Pandas DataFrame
Example 1:
Output:
The original DataFrame: Brands ... Price 1 Ford ... 900000 2 Toyota ... 4000000 3 Renault ... 1400000 [3 rows x 3 columns] DataFrame after deletion: Brands Cars 1 Ford Ecosport 2 Toyota Fortunar 3 Renault DusterExample 2:
Output:
Data before deleting column... Name Age 0 Alvin 21 1 Alex 22 2 Peter 19 Data after deleting column... Name 0 Alvin 1 Alex 2 Peterneed an explanation for this answer? contact us directly to get an explanation for this answer