Menu Close

How to Delete a column in Pandas DataFrame

How to Delete a column in Pandas DataFrame

In this article, we are going to show how to delete a column in Pandas DataFrame with the help of various examples. In previous tutorials, we have seen all about how to add a column in Pandas DataFrame with the help of four different-2 ways.

What is Dataframe in Python ?

In Python, Pandas DataFrame is a two-dimensional array that stores data in the form of rows and columns. Python provides popular package pandas that are used to create the DataFrame in Python.

As you can see below example that is a perfect example of Pandas DataFrame.

Example


import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# defining new list that reprsent the value 
address = ["Delhi", "Lucknow", "Mumbai", "Bangalore"]

# add address column
data = {
    "Noida": "Vishvajit Rao", "Bangalore": "John", "Harshita": "Pune", "Mohak": "Delhi"
}

# adding new column address
df["Address"] = data

# print
print(df)

Output


            Name  age           Occupation                   Skills    Address
0  Vishvajit Rao   23            Developer                   Python      Noida
1           John   33  Front End Developer                  Angular  Bangalore
2       Harshita   21               Tester                 Selenium   Harshita
3          Mohak   30           Full Stack  Python, React and MySQL      Mohak

How to delete a column in Pandas DataFrame

Here we will see a total of two ways to drop a column in Pandas DataFrame.

Using DataFrame drop() method

Python DataFrame provides a method drop() that is used to drop a column from DataFrame.

Example: Drop a column in Pandas DataFrame

import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# dropping a column in Dataframe
df.drop(["Skills"], inplace=True, axis=1)

# print
print(df)

Output


0  Vishvajit Rao   23            Developer
1           John   33  Front End Developer
2       Harshita   21               Tester
3          Mohak   30           Full Stack

Code Explanation

  • Importing pandas as pd
  • Define list of students objects.
  • Convert into Dataframe
  • Droping Skills columns from DataFrame.If inplace set True than operation will perform on original object and return None and after that axes set 1 that represent column.
  • print DataFrame.

Using del Keyword

Here we are going to delete a column in Pandas DataFrame using the del keyword.

Example: Delete a column in DataFrame Python


import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# deleting Skills column
del df["Skills"]

# print
print(df)

Output


            Name  age           Occupation
0  Vishvajit Rao   23            Developer
1           John   33  Front End Developer
2       Harshita   21               Tester
3          Mohak   30           Full Stack

Using Dataframe pop() method

Python Dataframe also provides the best method pop() to delete a column in DataFrame Python. let’s see how can we use the pop() method with the help of a proper example.

Example: Drop a column in Pandas DataFrame


import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# deleting Skills column
df.pop("Skills")

# print
print(df)

Output


            Name  age           Occupation
0  Vishvajit Rao   23            Developer
1           John   33  Front End Developer
2       Harshita   21               Tester
3          Mohak   30           Full Stack

How to Drop Multiple Columns in Pandas DataFrame

As you can see in the above examples we have only seen deleting a single column from Pandas DataFrame. But we can also delete multiple columns in Pandas DataFrame using the DataFrame drop() method.

Example: Delete multiple columns in DataFrame Python


import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# dropping a column in Dataframe
df.drop(["Occupation", "Skills"], inplace=True, axis=1)

# print
print(df)

Output

            Name  age
0  Vishvajit Rao   23
1           John   33
2       Harshita   21
3          Mohak   30

Conclusion

So in this article, we have gone through a total of three ways to delete a column from Pandas DataFrame with the help of various examples. You can use anyone from all these awesome techniques to delete a column from Dataframe Python.

You can also drop multiple columns in Pandas DataFrame with the help of the drop() method by defining all the column names.

I hope this article will help you, if you like this article, please share and keep visiting for further Python Pandas tutorials.

Related Articles:-

Thanks for your valuable time …. 👏👏👏

Previous Article

Related Posts