How to delete column in Pandas

As the world of data is growing, corporation are maintaining detailed datasets. Number of columns are increasing day by day. It becomes sometime very difficult to work with data having multiple columns in it. So there exist a need of deleting non required columns. This article covers how to delete columns in Pandas in Python. Below example explains step by step process on how to delete column in Pandas Python.

Emma and John have the below customer data file available with them. Both are looking forward to work with the data. While Emma wants to work with the data after removing Last Name column, John is looking forward to work with much smaller table. John wants to drop Last Name and Country as well.

Delete column in Pandas

Preparing datafile for Emma.

  • Step 1: Import all the necessary modules like Pandas.
import pandas as pd
  • Step 2: Use drop function to drop the necessary column. The first attribute of drop is the column list. As Emma needs to drop only one column so [‘Last Name’] is used here. Axis = 1 tells to drop data at column level.
df1=Customer_data_Pandasdf.drop(['Last Name'], axis = 1)
  • Step 3: Check sample data of new file if correct data is generated
df1.head() 
Delete column in Pandas

Preparing datafile for John.

  • Step 1: Importing all the key modules/ libraries.
import pandas as pd
  • Step 2: Here John needs to drop 2 columns. So the first attribute in drop function will have [‘Last Name’, ‘Country’], axis will be again set to 1.
df2=Customer_data_Pandasdf.drop(['Last Name','Country'], axis = 1)
  • Step 3: Testing the final file if the new data looks good to use.
df2.head()
Delete column pandas 3

Thus, Emma is able to delete column in Dataframe as per her requirement in Python.

To get top certifications in Python and build your resume visit here. Also, you can read books listed here to build strong knowledge around Python.

Visit us below for video tutorial:

 Looking to practice more with this example? Drop us a note, we will email you the Code file: 

    📬 Stay Ahead in Data Science & AI – Subscribe to Newsletter!

    • 🎯 Interview Series: Curated questions and answers for freshers and experienced candidates.
    • 📊 Data Science for All: Simplified articles on key concepts, accessible to all levels.
    • 🤖 Generative AI for All: Easy explanations on Generative AI trends transforming industries.

    💡 Why Subscribe? Gain expert insights, stay ahead of trends, and prepare with confidence for your next interview.

    👉 Subscribe here:

    Related Posts