Python

Pandas Drop Column

This article will discuss various methods you can use to delete a column in a Pandas DataFrame.

Method 1 – Pandas Drop()

Pandas provide the drop() method, which allows us to remove a column based on the column label and its corresponding axis.

The function syntax is as shown below:

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

The function parameters are as described below:

  1. Labels – specifies the index or the column’s label to delete. You can pass a single value or a list-like object.
  2. axis – specifies which axis the selected column label is to be deleted.
  3. index – alternative to the axis.
  4. columns – alternative to the axis.
  5. Inplace – specifies if the operation is to be performed in-place or not.
  6. errors – sets the action when errors arise. Accepted values are ‘raise’ and ‘ignore,’ which raises errors when they occur and suppresses errors.

Let us illustrate how we can use this function to drop a column.

Start by creating a simple pandas DataFrame as shown in the example below:

# import pandas
import pandas as pd

# create dataframe
df = pd.DataFrame({
    'db_type': ['relational', 'key-value', 'document', 'search-engine'],
    'default_port': [3306, 6379, 27017, 9200]
},

index=['MySQL', 'Redis', 'MongoDB', 'Elasticsearch'])
df

The code above should return a DataFrame as shown:

To delete the Redis entry from the DataFrame, we can run the code:

print(df.drop('Redis', inplace=False, axis=0))

In the example above, we use the drop() function to delete the column ‘Redis’ from the DataFrame.

Note: We set the in-place parameter to false. This prevents the function from modifying the original DataFrame.

NOTE: Setting the axis=0 will remove a row instead of a column. If you wish to remove a column, set the axis parameter to one.

This should return:

Method 2 – Pop

You can also use the pop() method to remove a column from a DataFrame. An example is illustrated below:

df.pop('default_port')

In this case, the function will remove the ‘default_port’ column from the DataFrame.

Closing

This tutorial covers two primary methods you can use to remove a column and rows from a Pandas DataFrame. Feel free to explore the docs for more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list