It is a common situation when we have to drop one or multiple columns in Pandas Dataframe. The Pandas library offers us a built-in function, DataFrame.drop(), to do it easily.

how to drop a column in Pandas

Prerequisite

Any example in Pandas will start with creating a DataFrame. We assume that you have the essential knowledge of Pandas. If you haven’t, you can visit our Python Pandas for Beginner Tutorial. And now, creating a new DataFrame for the examples:

import numpy as np
import pandas as pd

dates = pd.date_range('20191001', periods=6)
dataframe = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
print(dataframe)

"""
                 A         B         C         D
2019-10-01  -1.417066 -0.847748  1.186001  0.545916
2019-10-02  -1.513948 -0.001457  0.028132 -0.632664
2019-10-03   1.778671  0.171043  1.930001  0.358926
2019-10-04   1.654355  0.725009 -1.721475 -1.029845
2019-10-05  -0.605546 -0.051937 -0.343307  2.143983
2019-10-06  -1.024893  1.086735  1.345052  2.546996
"""

The Syntax

After creating the sample Dataframe, we should take a look at the syntax of drop(). Make sure you read it and understand it before let “your hand dirty” by the examples.

drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

We mainly use the first four parameters, so we will explain it below. Other parameters will be in the official document of Pandas (DataFrame.drop()).

  • labels: the labels to be dropped. Should be index or column label.
  • axis: 0 – drop by index, 1 – drop by columns
  • index: single or list of the index to be dropped. An alternative for axis and labels. (labels, axis=0) is equivalent to index=labels
  • columns: single or list of columns to be dropped. An alternative for axis and labels. (labels, axis=1) is equivalent to columns=labels

Example 1: Drop A Column In Pandas

In this example, we will drop a column labeled ‘C’ by using the labels and axis parameter.

dataframe.drop(['C'], axis=1)

"""
                   A         B         D
2019-10-01   -1.417066 -0.847748  0.545916
2019-10-02   -1.513948 -0.001457 -0.632664
2019-10-03    1.778671  0.171043  0.358926
2019-10-04    1.654355  0.725009 -1.029845
2019-10-05   -0.605546 -0.051937  2.143983
2019-10-06   -1.024893  1.086735  2.546996
"""

Example 2: Using columns param

Instead of using the labels and axis, we can drop columns directly by using columns param. For example, we want to drop column “B” and “C”.

dataframe.drop(columns=['B', 'C'])

                   A         D
2019-10-01    -1.417066  0.545916
2019-10-02    -1.513948 -0.632664
2019-10-03     1.778671  0.358926
2019-10-04     1.654355 -1.029845
2019-10-05    -0.605546  2.143983
2019-10-06    -1.024893  2.546996

Conclusion

This is a short tutorial to quickly answer the question “how to drop a column in Pandas”. Hopefully, after the above examples, you can use the drop() function in your code. However, if you want to fully understand the basic of Pandas library, don’t hesitate to visit our article, Python Pandas for Beginner.

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *