How to remove columns or rows of a DataFrame in pandas

Overview

The drop() function in pandas removes the rows or columns of a DataFrame.

  • To drop a column, we specify the label name and/or the corresponding axis of the column.
  • To drop a row, we specify its index position.

Syntax

The drop() function has the syntax shown below:

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

Parameters

The drop() function takes the following parameter values:

  • labels: This represents either the index label to remove a row or a column label to remove a column. It is equivalent to the index parameter.
  • axis: This takes the axis of the DataFrame to drop. The value 0 is for the index while 1 is for the column.
  • index: This is equivalent to the labels parameter.
  • columns: This takes the column label to be removed from the DataFrame.
  • level: This is used for multiIndex DataFrame. It specifies the level from which the row of the DataFrame should be removed.
  • inplace: This takes a boolean value and specifies whether a copy of the result should be copied or not.
  • errors: This takes either ignore or raise as its values. If its value is set to ignore, it avoids the error and returns a DataFrame whose existing labels are dropped.

Return value

The drop() function returns a DataFrame. If the parameter inplace is set to True, it returns None.

Example 1: Removing or dropping a row or a column

# A code to illustrate the drop() function
# importing required module
from pandas import DataFrame
# creating a dataframe
my_data_frame = DataFrame({'Id': [1, 2, 3, 4, 5, 6],
'Name': ["Theo", "James", "John", "Peter", "Paul", "Hamed"] ,
'Hieght(m)': [1.83, 1.98, 1.78, 1.8, 1.79, 2.0]})
print(my_data_frame)
print("\n")
# dropping the "Id" and "Name" columns
print(my_data_frame.drop(["Id", "Name"], axis = 1))
# dropping the first and fifth rows
print(my_data_frame.drop([0, 4]))

Explanation

  • Line 4: We import the Dataframe module from the pandas library.
  • Line 7: We create a DataFrame my_data_frame.
  • Line 10: We print my_data_frame.
  • Line 15: We drop the columns with the label names "Id" and "Name" by using the drop() function. We print the result to the console.
  • Line 18: We drop the rows with index positions 0 and 4 using the drop() function. We print the result to the console.

Example 2: Removing or dropping a row from a MultiIndex DataFrame

from pandas import MultiIndex, DataFrame
# creating a multiIndex dataframe
my_multi_index = MultiIndex(levels=[['Tiger', 'Cow', 'Buffalo'],
['Speed', 'Weight', 'Length']],
codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
[0, 1, 2, 0, 1, 2, 0, 1, 2]])
my_data_frame = DataFrame(index=my_multi_index, columns=['Big', 'Small'],
data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
[250, 150], [1.5, 0.8], [320, 250],
[1, 0.8], [0.3, 0.2]])
print(my_data_frame)
print("\n")
# dropping a column
print("Dropping the 'Big' Column")
print(my_data_frame.drop(columns = ["Big"]))
print("\n")
# dropping a row
print("Dropping the 'Tiger' Row")
print(my_data_frame.drop(index = "Tiger"))

Example

  • Lines 4–8: We create a MultiIndex my_multi_index and a MultiIndex DataFrame my_data_frame.
  • Line 12: We print my_data_frame.
  • Line 17: Using the drop() function, we drop the "Big" column.
  • Line 22: Using the drop() function, we drop the "Tiger" column.

Free Resources