How to Remove Rows from DataFrame in Pandas

Here are five ways to remove rows from DataFrame in Python:

  1. Using drop() method
  2. Using boolean indexing
  3. Using the query() method
  4. Using the iloc[] indexer
  5. Using the drop() method with the index attribute

Method 1: Using the drop() method

To remove single or multiple rows from a DataFrame in Pandas, you can use the drop() method by specifying the index labels of the rows you want to remove.

import pandas as pd

# Create a sample DataFrame
df = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12]}
df = pd.DataFrame(df)

# Remove row with index label 1
df = df.drop(1)
print("Removing a single row")
print(df)

# Remove rows with index labels 2 and 3
df = df.drop([2, 3])
print("Removing multiple rows")
print(df)

Output

Removing a single row
   A  B  C
0  1  5  9
2  3  7  11
3  4  8  12
Removing multiple rows
   A  B  C
0  1  5  9

Method 2: Using boolean indexing

You can use boolean indexing to filter out the rows you want to remove based on a condition.

import pandas as pd

# Create a sample DataFrame
df = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12]}
df = pd.DataFrame(df)

# Remove rows where the 'B' column is less than or equal to 6
df = df[df['B'] > 6]

print(df)

Output

   A  B  C
2  3  7  11
3  4  8  12

Method 3: Using the query() method

You can use the query() method to remove rows based on a query expression.

import pandas as pd

# Create a sample DataFrame
df = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12]}
df = pd.DataFrame(df)

# Remove rows where the 'C' column is greater than than 9
df = df.query('C > 9')

print(df)

Output

   A  B  C
1  2  6  10
2  3  7  11
3  4  8  12

Method 4: Using the iloc[] indexer

Use the iloc[] indexer to select rows based on their integer locations.

import pandas as pd

# Create a sample DataFrame
df = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12]}
df = pd.DataFrame(df)

# Remove the three rows
df = df.iloc[1:]

print(df)

Output

   A  B  C
1  2  6  10
2  3  7  11
3  4  8  12

Method 5: Using the drop() method with the index attribute

You can remove rows by their index label(s) with the drop() method combined with the index attribute.

import pandas as pd

# Create a sample DataFrame
df = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12]}
df = pd.DataFrame(df)

# Remove the row with the index label '0'
df = df.drop(df.index[0])

# Remove the rows with index labels '1' and '2'
df = df.drop(df.index[[1, 2]])

print(df)

Output

   A  B  C
1  2  6  10

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.