How to Drop Pandas Rows
-
Drop Rows by the Index in the
pandas.DataFrame.drop()
Method - Drop Rows Depending Upon the Value of a Particular Column in Pandas DataFrame
This tutorial explains how we can drop rows in Pandas using the pandas.DataFrame.drop()
method.
import pandas as pd
kgp_df = pd.DataFrame(
{
"Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
"Age": [30, 33, 35, 30, 30],
"Weight(KG)": [75, 75, 80, 70, 73],
}
)
print("The KGP DataFrame is:")
print(kgp_df)
Output:
The KGP DataFrame is:
Name Age Weight(KG)
0 Himansh 30 75
1 Prateek 33 75
2 Abhishek 35 80
3 Vidit 30 70
4 Anupam 30 73
We will use the kgp_df
DataFrame to explain how we can drop rows from the Pandas DataFrame.
Drop Rows by the Index in the pandas.DataFrame.drop()
Method
import pandas as pd
kgp_df = pd.DataFrame(
{
"Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
"Age": [30, 33, 35, 30, 30],
"Weight(KG)": [75, 75, 80, 70, 73],
}
)
rows_dropped_df = kgp_df.drop(kgp_df.index[[0, 2]])
print("The KGP DataFrame is:")
print(kgp_df, "\n")
print("The KGP DataFrame after dropping 1st and 3rd DataFrame is:")
print(rows_dropped_df)
Output:
The KGP DataFrame is:
Name Age Weight(KG)
0 Himansh 30 75
1 Prateek 33 75
2 Abhishek 35 80
3 Vidit 30 70
4 Anupam 30 73
The KGP DataFrame after dropping 1st and 3rd DataFrame is:
Name Age Weight(KG)
1 Prateek 33 75
3 Vidit 30 70
4 Anupam 30 73
It drops the rows with indices 0 and 2 from the kgp_df
DataFrame. The rows with indices 0 and 2 correspond to the first and third row in the DataFrame because the index starts from 0.
We can also use the index of the DataFrame to delete the rows instead of using the default index.
import pandas as pd
kgp_idx = ["A", "B", "C", "D", "E"]
kgp_df = pd.DataFrame(
{
"Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
"Age": [30, 33, 35, 30, 30],
"Weight(KG)": [75, 75, 80, 70, 73],
},
index=kgp_idx,
)
rows_dropped_df = kgp_df.drop(["A", "C"])
print("The KGP DataFrame is:")
print(kgp_df, "\n")
print("The KGP DataFrame after dropping 1st and 3rd DataFrame is:")
print(rows_dropped_df)
Output:
The KGP DataFrame is:
Name Age Weight(KG)
A Himansh 30 75
B Prateek 33 75
C Abhishek 35 80
D Vidit 30 70
E Anupam 30 73
The KGP DataFrame after dropping 1st and 3rd DataFrame is:
Name Age Weight(KG)
B Prateek 33 75
D Vidit 30 70
E Anupam 30 73
It drops the rows with index A
and C
, or the first and third row from the DataFrame.
We pass the list of index of the rows to be dropped to the drop()
method to delete the respective rows.
Drop Rows Depending Upon the Value of a Particular Column in Pandas DataFrame
import pandas as pd
kgp_idx = ["A", "B", "C", "D", "E"]
kgp_df = pd.DataFrame(
{
"Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
"Age": [31, 33, 35, 36, 34],
"Weight(KG)": [75, 75, 80, 70, 73],
},
index=kgp_idx,
)
young_df_idx = kgp_df[kgp_df["Age"] <= 33].index
young_folks = kgp_df.drop(young_df_idx)
print("The KGP DataFrame is:")
print(kgp_df, "\n")
print("The DataFrame of folks with age less than or equal to 33 are:")
print(young_folks)
Output:
The KGP DataFrame is:
Name Age Weight(KG)
A Himansh 31 75
B Prateek 33 75
C Abhishek 35 80
D Vidit 36 70
E Anupam 34 73
The DataFrame of folks with age less than or equal to 33 are:
Name Age Weight(KG)
C Abhishek 35 80
D Vidit 36 70
E Anupam 34 73
It will drop all the rows with age less than or equal to 33.
We first find the index of all the rows with age less than or equal to 33 and then drop the rows using the drop()
method.
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedInRelated Article - Pandas DataFrame Row
- How to Get the Row Count of a Pandas DataFrame
- How to Randomly Shuffle DataFrame Rows in Pandas
- How to Filter Dataframe Rows Based on Column Values in Pandas
- How to Iterate Through Rows of a DataFrame in Pandas
- How to Get Index of All Rows Whose Particular Column Satisfies Given Condition in Pandas
- How to Find Duplicate Rows in a DataFrame Using Pandas