18 Apr Drop Rows in Pandas
In this lesson, we will see how to drop rows. Dropping rows in pandas is a fundamental part of data cleaning. It involves removing unwanted, incorrect, or missing data to improve the quality of your dataset. In pandas, the primary method for removing rows is the drop() method.
Let us see how to:
- Drop a row
- Drop multiple rows
Drop a row in Pandas
Let us see how to drop a row in Pandas using the drop() method:
# Drop a Row
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8],
'C': [9, 10, 11, 12]})
print(df)
# Remove a row with index 0
df.drop(0, inplace=True)
print("\nUpdated DataFrame\n",df)
Output
A B C 0 1 5 9 1 2 6 10 2 3 7 11 3 4 8 12 Updated DataFrame A B C 1 2 6 10 2 3 7 11 3 4 8 12
Drop multiple rows in Pandas
Let us see how to drop multiple rows in Pandas using the drop() method:
# Drop multiple rows
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8],
'C': [9, 10, 11, 12]})
print(df)
# Remove multiple rows with index 0 and 2
df.drop([0, 2], inplace=True)
print("\nUpdated DataFrame\n",df)
Output
A B C 0 1 5 9 1 2 6 10 2 3 7 11 3 4 8 12 Updated DataFrame A B C 1 2 6 10 3 4 8 12
If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments