How to Drop Rows in Pandas

Using pandas.DataFrame.drop() we may drop, remove, or eliminate rows we want from the given DataFrame. We may choose the axis to delete using the DataFrame.axis param. Axis = 0 by default means to eliminate rows. To eliminate columns, apply axis = 1 or columns_param. When removing rows, Pandas, by default, creates a copy of the DataFrame; to delete from an existing referencing DataFrame, use the inplace = True option.

Syntax:

Parameters:

  • labels:Row or column title referenced by a string or set of strings.
  • axis:Int or string item, with a row index of 0 and a column index of 1, respectively.
  • index or columns:Individual label or list. An index or column can replace the label. However, they cannot be employed jointly.
  • level:When a data frame has multiple level indexes, this is used to define the level.
  • inplace:If True, updates the original Data Frame.
  • errors: If any item in the list is false, the error is ignored, and the remaining values are dropped when errors are set to "ignore."

Return type: Updated DataFrame

Delete a Single Row Using the Row Index Label

One advantage of pandas is providing rows labels or titles similar to column names. We can specify individual rows to remove using the row label names if our data frame supports row labeling (also called index labeling).

Code

Output:

Original DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32
B  Peter   26            20
C  Harry   28            38
D  Naill   25            17
New DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32
C  Harry   28            38
D  Naill   25            17

Delete Multiple Rows Using Index Labels

We can give multiple row-index labels in a list to the drop command to delete multiple rows from the DataFrame.

Code

Output:

Original DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32
B  Peter   26            20
C  Harry   28            38
D  Naill   25            17
New DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32
D  Naill   25            17

Drop Rows by Index Number

Likewise, we can delete rows from a given pandas DataFrame by providing index position as arguments to the drop() method. We must extract the row names using the indices and pass them to the drop() method since it does not accept the position index of rows as a parameter. To obtain row labels of a DataFrame for the indices we intended to delete, we will use df.index.

All row labels are returned as a list using the df.index.values function.

For instance, using df.index[[1,2]], the row labels for the second and third rows are obtained; the drop() method then deletes these rows. Keep in mind that the indexing of lists in Python starts at zero.

Code

Output:

Original DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32
B  Peter   26            20
C  Harry   28            38
D  Naill   25            17
New DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32
D  Naill   25            17

Delete rows of a DataFrame in Place

Until now, we have been getting a new DataFrame with the rows deleted. However, we can delete the rows of a DataFrame without creating a new one called performing 'in place' operations on the DataFrame.

Code

Output:

Original DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32
B  Peter   26            20
C  Harry   28            38
D  Naill   25            17
Updated DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32
D  Naill   25            17

Delete Rows Using Index Range

By defining the index range, we may also eliminate rows. The instance below deletes all rows that exist before the third row.

Code

Output:

Original DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32
B  Peter   26            20
C  Harry   28            38
D  Naill   25            17
Updated DataFrame: 
     Name  Age  Salary (LPA)
D  Naill   25            17

Drop Rows by Checking Conditions

Through the loc[] and iloc[] functions, we can easily remove DataFrame rows according to certain conditions (column value).

Code

Output:

Original DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32
B  Peter   26            20
C  Harry   28            38
D  Naill   25            17
New DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21            32

Drop Rows Containing NaN/None Values

We will frequently need to clean up rows that contain None, Null, and np.NaN values when dealing with analytics. We can delete NaN values from a given DataFrame by calling df.dropna().

Code

Output:

Original DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21          32.0
B  Peter   26           NaN
C  Harry   28          38.0
D  Naill   25           NaN
New DataFrame: 
     Name  Age  Salary (LPA)
A  Itika   21          32.0
C  Harry   28          38.0

Next Topic#




Latest Courses