DataFrame rows are based on the index values. We can manipulate both rows and columns in pandas, DataFrame.drop() method is used to delete any particular row or column based on the condition which is passed as a parameter. Here, we are going to use DataFrame.drop() to delete a row based on a column value condition.
pandas.DataFrame.drop() Method
This method is used to remove a specified row or column from the pandas DataFrame. Since rows and columns are based on index and axis values respectively, by passing the index or axis value inside DataFrame.drop() method we can delete that particular row or column. Below is the syntax:
Syntax:
DataFrame.drop(
labels=None,
axis=0,
index=None,
columns=None,
level=None,
inplace=False,
errors='raise'
)
# short forms
df.drop(axis=None) # deletes a specified column
df.drop(index=None) # deletes a specified row
To work with Python Pandas, we need to import the pandas library. Below is the syntax,
import pandas as pd
Example:
Output:
Original DataFrame: Name Age Gender Profession Title 0 Hari 25 Male Doctor Mr 1 Mohan 36 Male Teacher Mr 2 Neeti 26 Female Singer Ms 3 Shaily 21 Female Student Ms 4 Ram 30 Male Engineer Mr 5 Umesh 33 Male CA Mr Updated DataFrame: Name Age Gender Profession Title 0 Hari 25 Male Doctor Mr 1 Mohan 36 Male Teacher Mr 4 Ram 30 Male Engineer Mr 5 Umesh 33 Male CA Mr
need an explanation for this answer? contact us directly to get an explanation for this answer