Delete Column from Pandas DataFrame

[toc]

Problem Statement: Delete columns from pandas DataFrame.

Have you been wondering – “How to delete a column from pandas DataFrame?“. Well, this tutorial will answer your queries.

🐼A Quick Recap to Pandas Dataframe

Pandas Dataframe is a two-dimensional data structure that stores values in a tabular format. It is immutable and heterogeneous. It has labeled axes, rows and columns.

  • Pandas Dataframe comprises three parts:
    • data,
    • rows, and
    • columns.

Let’s create a Dataframe to understand this:

import pandas as pd
df = pd.DataFrame({
    'col1': [10, 50, 80], 
    'col2': ['a', 'b', 'c'],
    'col3': [True, False, True],
})
print(df)

Output:

   col1 col2   col3
0    10    a   True
1    50    b  False
2    80    c   True

That was a quick recap to a pandas dataframe in Python. Now that we have successfully created our Dataframe, let’s dive into the different methods on how to delete a column from it.

πŸ“ΉVideo Walkthrough

✨Method 1: Using del

You can delete a specific column using the del keyword. However, you must remember that this method only works if we want to delete a single column. If we want to delete multiple columns at a time we cannot use the del keyword to do so.

Syntax:

del df['column name']

Example:

import pandas as pd
df = pd.DataFrame({
    'col1': [10, 50, 80],
    'col2': ['a', 'b', 'c'],
    'col3': [True, False, True],
})
print("The DataFrame before deleting the column")
print(df)
# Deleting column 2 from the dataframe
del df["col2"]
print("The DataFrame after deleting the column")
print(df)

Output:

The DataFrame before deleting the column
   col1 col2   col3
0    10    a   True
1    50    b  False
2    80    c   True
The DataFrame after deleting the column
   col1   col3
0    10   True
1    50   False
2    80   True

Caution: It is a common mistake among many beginners who just type del df.col2 to delete a column. Please note that this will not work. You have to follow the syntax identical to the one shown above.

✨Method 2: Using pop()

You can delete columns from pandas dataframe by using the DataFrameDataFrame.pop() method. This method returns the deleted column. The method also deletes the column from the original DataFrame.

Syntax:

df.pop('column name')

Example:

import pandas as pd
df = pd.DataFrame({
    'col1': [10, 50, 80],
    'col2': ['a', 'b', 'c'],
    'col3': [True, False, True],
})
print("The DataFrame before deleting the column:")
print(df)
# Deleting column 3 from the dataframe
df.pop("col3")
print("The DataFrame after deleting the column:")
print(df)

Output:

The DataFrame before deleting the column:
   col1 col2   col3
0    10    a   True
1    50    b  False
2    80    c   True
The DataFrame after deleting the column:
   col1 col2
0    10    a
1    50    b
2    80    c

✨Method 3: Using .drop()

pandas.DataFrame.drop is another method that is popularly used to delete the specified labels from either rows or columns.

Syntax:

df.drop(axis = 1, inplace = True);

Note: When we need to delete the columns, we need to specify the axis as 1. (0 is used for rows and 1 is used for columns.)

Example:

import pandas as pd
df = pd.DataFrame({
    'col1':[10, 50, 80],
    'col2': ['a', 'b', 'c'],
    'col3': [True, False, True],
})
print("The DataFrame before deleting the column:")
print(df)
# Deleting column 1 from the dataframe
df = df.drop(['col1'], axis = 1)
print("The DataFrame after deleting the column:")
print(df)

Output:

The DataFrame before deleting the column:
   col1 col2   col3
0    10    a   True
1    50    b  False
2    80    c   True
The DataFrame after deleting the column:
  col2   col3
0    a   True
1    b   False
2    c   True

➑ We can also delete multiple columns using the drop() method.

Example:

import pandas as pd
df = pd.DataFrame({
    'col1': [10, 50, 80], 
    'col2': ['a', 'b', 'c'],
    'col3': [True, False, True],
})
print("The DataFrame before deleting the column:")
print(df)
# Deleting column 1 and column 3 from the dataframe
df = df.drop(['col1', 'col3'], axis = 1)
print("The DataFrame after deleting the column:")
print(df)

Output:

The DataFrame before deleting the column:
col1 col2   col3
0    10    a   True
1    50    b  False
2    80    c   True
The DataFrame after deleting the column:
col2
0    a
1    b
2    c

Note: We can also delete the columns without re-assigning the result back to df, by just specifying the inplace to be True.

✨Method 4: Deleting Columns by their Index

We can also delete the columns by their index in the DataFrame using the drop() method rather than using their column name. It proves to be really useful if the columns in the DataFrame are not named or if there is more than one column with the same name. 

Syntax:

df.drop(df.columns[column index], axis = 1, inplace = True)

Here, we need to set the labels to pd.DataFrame.columns[x] where x is the column index that needs to be deleted.

Example:

import pandas as pd

df = pd.DataFrame({
    'col1': [10, 50, 80],
    'col2': ['a', 'b', 'c'],
    'col3': [True, False, True],
})
print("The DataFrame before deleting the column:")
print(df)
# Deleting the column with index 0 from the dataframe
df.drop(df.columns[0], axis=1, inplace=True)
print("The DataFrame after deleting the column:")
print(df)

Output:

The DataFrame before deleting the column:
   col1 col2   col3
0    10    a   True
1    50    b  False
2    80    c   True
The DataFrame after deleting the column:
  col2   col3
0    a   True
1    b  False
2    c   True

In the above example, as col1 has index 0, it has been deleted from the Dataframe. We can also delete multiple columns from the Dataframe using their indexes. In the below example, we will delete the columns with index 0 and index 2:

Example:

import pandas as pd

df = pd.DataFrame({
    'col1': [10, 50, 80],
    'col2': ['a', 'b', 'c'],
    'col3': [True, False, True],
})
print("The DataFrame before deleting the column:")
print(df)
# Deleting the column with index 0 and index 2 from the dataframe
df.drop(df.columns[[0, 2]], axis=1, inplace=True)
print("The DataFrame after deleting the column:")
print(df)

Output:

The DataFrame before deleting the column:
   col1 col2   col3
0    10    a   True
1    50    b  False
2    80    c   True
The DataFrame after deleting the column:
  col2
0    a
1    b
2    c

Conclusion

That was all about the different methods to delete a column from a Pandas dataframe. Drop-in your queries and let us know if this article helped you. If you wish to receive daily solutions and concepts to strengthen your Python skills, please subscribe.

Want to get started with Pandas in 10 mins? Follow this tutorial : 10 Minutes to Pandas [FINXTER]


Learn Pandas the Fun Way by Solving Code Puzzles

If you want to boost your Pandas skills, consider checking out my puzzle-based learning book Coffee Break Pandas (Amazon Link).

Coffee Break Pandas Book

It contains 74 hand-crafted Pandas puzzles including explanations. By solving each puzzle, you’ll get a score representing your skill level in Pandas. Can you become a Pandas Grandmaster?

Coffee Break Pandas offers a fun-based approach to data science mastery—and a truly gamified learning experience.

Leave a Comment