• Offers
    • RegisterLogin
      • Learn More
    PythonPoint.netPythonPoint.net
    • Offers
    • RegisterLogin
      • Learn More

      Python

      SKILL IS IMPORTANT THAN DEGREE Be skill full.
      • Home
      • Blog
      • Python
      • How to drop a column in Python

      How to drop a column in Python

      • Posted by Python Point Team
      • Categories Python
      • Date November 30, 2020
      • Comments 0 comment
      how to drop a column in python

      In this article, lets discuss how to drop a column in Pandas Dataframe in Python.

      For demonstrating this, first lets create a dummy dataframe.

      import pandas as pd 
      
      data = {
          'A':['a1', 'a2', 'a3', 'a4', 'a5'],  
          'B':['b1', 'b2', 'b3', 'b4', 'b5'],  
          'C':['c1', 'c2', 'c3', 'c4', 'c5'],  
          'D':['d1', 'd2', 'd3', 'd4', 'd5'],  
          'E':['e1', 'e2', 'e3', 'e4', 'e5']
      }
      
      df = pd.DataFrame(data)
      
      df

      Output:

          A   B   C   D   E
      0  a1  b1  c1  d1  e1
      1  a2  b2  c2  d2  e2
      2  a3  b3  c3  d3  e3
      3  a4  b4  c4  d4  e4
      4  a5  b5  c5  d5  e5

      So now we’ve got the dataframe. Now let’s use drop() method to drop a column from the dataframe. By using this method, we can drop a specific single column.

      import pandas as pd 
      
      data = {
          'A':['a1', 'a2', 'a3', 'a4', 'a5'],  
          'B':['b1', 'b2', 'b3', 'b4', 'b5'],  
          'C':['c1', 'c2', 'c3', 'c4', 'c5'],  
          'D':['d1', 'd2', 'd3', 'd4', 'd5'],  
          'E':['e1', 'e2', 'e3', 'e4', 'e5']
      }
      
      df = pd.DataFrame(data)
      
      #remove column name 'A'
      print(df.drop(['A'], axis = 1))
      

      Output:

          B   C   D   E
      0  b1  c1  d1  e1
      1  b2  c2  d2  e2
      2  b3  c3  d3  e3
      3  b4  c4  d4  e4
      4  b5  c5  d5  e5

      Remove column based on column index:

      import pandas as pd 
      
      data = {
      	'A':['a1', 'a2', 'a3', 'a4', 'a5'],  
          'B':['b1', 'b2', 'b3', 'b4', 'b5'],  
          'C':['c1', 'c2', 'c3', 'c4', 'c5'],  
          'D':['d1', 'd2', 'd3', 'd4', 'd5'],  
          'E':['e1', 'e2', 'e3', 'e4', 'e5']
      }
      
      df = pd.DataFrame(data)
      
      #removing column index 1, you can remove multiple column indices at a time
      df.drop(df.columns[[1]], axis = 1, inplace = True)
      
      df

      Output:

          A   C   D   E
      0  a1  c1  d1  e1
      1  a2  c2  d2  e2
      2  a3  c3  d3  e3
      3  a4  c4  d4  e4
      4  a5  c5  d5  e5
      • Share:
      author avatar
      Python Point Team

      Previous post

      What is lambda function in Python
      November 30, 2020

      Next post

      What is Variable in Python
      November 30, 2020

      You may also like

      Introduction to Transfer Learning with Python: A Practical Guide
      31 December, 2022

      Introduction: Definition of transfer learning Overview of how transfer learning works in the context of machine learning Why transfer learning is useful and important Section 1: Transfer learning in Python with Keras In this section, we will explore how to …

      How to Check Type in Python
      31 December, 2022

      In this article, we will learn to check type in Python. The built-in function type() can be used to check the type of data in Python.

      How to make web crawler in python?
      31 December, 2022

      A web crawler is a systematic, well-defined process of extracting specific data about a topic. We have a library named as scrapy which helps in making web crawler. We can install scrapy by the following command:- Now we can easily …

      Subscribe
      Login
      Notify of
      Please login to comment
      0 Discussion
      Inline Feedbacks
      View all comments

      Latest Courses

      (Hindi) Ways to earn minimum 1 Lakh Per month as Programmer

      (Hindi) Ways to earn minimum 1 Lakh Per month as Programmer

      ₹10,000
      (HINDI) Full Stack Web Development In Python 3.8 And Django 3.1

      (HINDI) Full Stack Web Development In Python 3.8 And Django 3.1

      ₹25,000 ₹2,500

      Latest Posts

      • Introduction to Transfer Learning with Python: A Practical Guide
      • How to Check Type in Python
      • How to make web crawler in python?
      • Why was the language called “python”?
      • Why Python is so Popular
      Contact
      •   support@pythonpoint.com

      We get you the best Python Courses and Blogs aiming to provide skill.

      We Believe Skill is much more important than a Degree

      Company
      • About Us
      • Blog
      • Offers
      • Contact
      Useful Links
      • Courses
      Support
      • Need Support

      © 2020 ALL RIGHTS RESERVED​ PYTHONPOINT.NET

      PythonPoint

      • Terms of Use
      • Refund Policy
      • Privacy Policy

      Login with your site account

      Lost your password?

      Not a member yet? Register now

      Register a new account

      Are you a member? Login now

      wpDiscuz