How to Drop a Row in Pandas


In Pandas, removing rows from a DataFrame is a crucial operation in data manipulation. Understanding how to remove single or multiple rows from a DataFrame is a must-have skill for any data analyst.

    Table of Contents

  1. Drop a Single Row
  2. Drop Multiple Rows
  3. Drop Multiple Rows in Range
  4. Drop Rows Based on Condition
  5. Conclusion

1. Drop a Single Row

To drop a single row from a DataFrame, we use the drop() method. It takes the index of the row to be dropped as an argument.

To drop 1st row from the DataFrame, use df.drop(0) or df.drop([0]).

import pandas as pd

# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['NY', 'LA', 'SF']}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# 👉 drop 1st row
df = df.drop(0)
print("\nDataFrame after dropping 1st row:")
print(df)

Output:

Original DataFrame:
      Name  Age City
0   Alice   25   NY
1     Bob   30   LA
2  Charlie   35   SF

DataFrame after dropping 1st row:
      Name  Age City
1     Bob   30   LA
2  Charlie   35   SF

2. Drop Multiple Rows

To drop multiple rows from a DataFrame, pass a list of indices to the drop() method.

For example to drop the 1st and 3rd row from the DataFrame, use df.drop([0, 2]).

import pandas as pd

# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['NY', 'LA', 'SF']}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# 👉 drop 1st and 3rd row
df = df.drop([0, 2])
print("\nDataFrame after dropping 1st and 3rd row:")
print(df)

Output:

Original DataFrame:
      Name  Age City
0   Alice   25   NY
1     Bob   30   LA
2  Charlie   35   SF

DataFrame after dropping 1st and 3rd row:
    Name  Age City
1   Bob   30   LA

3. Drop Multiple Rows in Range

Knowledge dropping multiple rows in a range is very important because we can have 10000s of rows in a DataFrame and we may need to drop rows from 1000 to 2000 or 4523 to 5678.

This can be done by using df.index to get the list of indices. For example to drop rows from 1 to 3, use df.drop(df.index[1:4]).

import pandas as pd

# Creating a sample DataFrame
data = {'A': [1, 2, 3, 4, 5],
        'B': [6, 7, 8, 9, 10],
        'C': [11, 12, 13, 14, 15]}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# 👉 drop rows from 1 to 3
df = df.drop(df.index[1:4])
print("\nDataFrame after dropping rows from 1 to 3:")
print(df)

Output:

Original DataFrame:
   A   B   C
0  1   6  11
1  2   7  12
2  3   8  13
3  4   9  14
4  5  10  15

DataFrame after dropping rows from 1 to 3:
   A   B   C
0  1   6  11
4  5  10  15

4. Drop Rows Based on Condition

To drop rows based on a condition, we first need to create a boolean array where True represents the rows to be dropped.

For example, to drop rows where the Age is greater than 30, we first create a boolean array where True represents the rows where Age is greater than 30.

import pandas as pd

# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['NY', 'LA', 'SF']}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# 👉 drop rows where Age is greater than 30
df = df.drop(df[df['Age'] > 30].index)
print("\nDataFrame after dropping rows where Age is greater than 30:")
print(df)

Output:

Original DataFrame:
      Name  Age City
0   Alice   25   NY
1     Bob   30   LA
2  Charlie   35   SF

DataFrame after dropping rows where Age is greater than 30:
    Name  Age City
0  Alice   25   NY
1    Bob   30   LA

Conclusion

For refining and cleaning the data, removing rows from a DataFrame is a very important operation. By now how to drop a single or multiple rows from a DataFrame using the drop() method.