Here are two ways to drop rows by the index in Pandas DataFrame:
(1) Drop a single row by index. For example, to drop the row that has an index of 2:
df.drop(index=2, inplace=True)
(2) Drop multiple rows by index. For instance, to drop the rows with the index values of 2, 4 and 6:
df.drop(index=[2, 4, 6], inplace=True)
The Example
To start, create a simple DataFrame with 7 rows:
import pandas as pd
data = {
"Product": ["Computer", "Printer", "Monitor", "Desk", "Phone", "Tablet", "Scanner"],
"Price": [900, 200, 300, 450, 150, 250, 150],
}
df = pd.DataFrame(data)
print(df)
As you can see, the index values are located on the left, starting from 0:
Product Price
0 Computer 900
1 Printer 200
2 Monitor 300
3 Desk 450
4 Phone 150
5 Tablet 250
6 Scanner 150
Drop a Single Row by Index in Pandas DataFrame
To drop a specific row, you’ll need to specify the associated index value that represents that row.
For example, to drop the row with the index of 2 (for the ‘Monitor’ product):
df.drop(index=2, inplace=True)
The complete code:
import pandas as pd
data = {
"Product": ["Computer", "Printer", "Monitor", "Desk", "Phone", "Tablet", "Scanner"],
"Price": [900, 200, 300, 450, 150, 250, 150],
}
df = pd.DataFrame(data)
df.drop(index=2, inplace=True)
print(df)
Once you run the code, you’ll no longer see the row with the index of 2:
Product Price
0 Computer 900
1 Printer 200
3 Desk 450
4 Phone 150
5 Tablet 250
6 Scanner 150
Drop Multiple Rows by Index in Pandas DataFrame
What if you want to drop multiple rows?
For instance, to drop the rows with the index values of 2, 4 and 6:
df.drop(index=[2, 4, 6], inplace=True)
Here is the complete Python code:
import pandas as pd
data = {
"Product": ["Computer", "Printer", "Monitor", "Desk", "Phone", "Tablet", "Scanner"],
"Price": [900, 200, 300, 450, 150, 250, 150],
}
df = pd.DataFrame(data)
df.drop(index=[2, 4, 6], inplace=True)
print(df)
You’ll now notice that the rows with the index values of 2, 4 and 6 are no longer present:
Product Price
0 Computer 900
1 Printer 200
3 Desk 450
5 Tablet 250
Index is a String
So far, you have seen how to drop rows where the index values are numeric.
But what if the index values are strings?
For example:
index=["Item_A", "Item_B", "Item_C", "Item_D", "Item_E", "Item_F", "Item_G"]
So the DataFrame with the new index values would be:
import pandas as pd
data = {
"Product": ["Computer", "Printer", "Monitor", "Desk", "Phone", "Tablet", "Scanner"],
"Price": [900, 200, 300, 450, 150, 250, 150],
}
df = pd.DataFrame(
data, index=["Item_A", "Item_B", "Item_C", "Item_D", "Item_E", "Item_F", "Item_G"]
)
print(df)
As you can see, the new non-numeric index values would appear on the left:
Product Price
Item_A Computer 900
Item_B Printer 200
Item_C Monitor 300
Item_D Desk 450
Item_E Phone 150
Item_F Tablet 250
Item_G Scanner 150
To drop two of those index values (“Item_B” and “Item_D”):
df.drop(index=["Item_B", "Item_D"], inplace=True)
Here is the complete syntax:
import pandas as pd
data = {
"Product": ["Computer", "Printer", "Monitor", "Desk", "Phone", "Tablet", "Scanner"],
"Price": [900, 200, 300, 450, 150, 250, 150],
}
df = pd.DataFrame(
data, index=["Item_A", "Item_B", "Item_C", "Item_D", "Item_E", "Item_F", "Item_G"]
)
df.drop(index=["Item_B", "Item_D"], inplace=True)
print(df)
You will no longer see the rows with the index values of “Item_B” and “Item_D”:
Product Price
Item_A Computer 900
Item_C Monitor 300
Item_E Phone 150
Item_F Tablet 250
Item_G Scanner 150
You can learn more about df.drop by visiting the Pandas Documentation.