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 theindexparameter.axis: This takes the axis of the DataFrame to drop. The value0is for theindexwhile1is for thecolumn.index: This is equivalent to thelabelsparameter.columns: This takes the column label to be removed from the DataFrame.level: This is used formultiIndexDataFrame. 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 eitherignoreorraiseas its values. If its value is set toignore, 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 modulefrom pandas import DataFrame# creating a dataframemy_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" columnsprint(my_data_frame.drop(["Id", "Name"], axis = 1))# dropping the first and fifth rowsprint(my_data_frame.drop([0, 4]))
Explanation
- Line 4: We import the
Dataframemodule from thepandaslibrary. - 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 thedrop()function. We print the result to the console. - Line 18: We drop the rows with index positions
0and4using thedrop()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 dataframemy_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 columnprint("Dropping the 'Big' Column")print(my_data_frame.drop(columns = ["Big"]))print("\n")# dropping a rowprint("Dropping the 'Tiger' Row")print(my_data_frame.drop(index = "Tiger"))
Example
- Lines 4–8: We create a MultiIndex
my_multi_indexand a MultiIndex DataFramemy_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.