How to drop rows in Pandas DataFrame by index labels?
Pandas provide data analysts a way to delete and filter data frame using .drop() method. Rows can be removed using index label or column name using this method.
DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=’raise’)
labels: String or list of strings referring row or column name.
axis: int or string value, 0 ‘index’ for Rows and 1 ‘columns’ for Columns.
index or columns: Single label or list. index or columns are an alternative to axis and cannot be used together.
level: Used to specify level in case data frame is having multiple level index.
inplace: Makes changes in original Data Frame if True.
errors: Ignores error if any value from the list doesn’t exists and drops rest of the values when errors = ‘ignore’
Return type: Dataframe with dropped values
Now, Let’s create a sample dataframe
Example #1: Delete a single Row in DataFrame by Row Index Label
Example #2: Delete Multiple Rows in DataFrame by Index Labels
Example #3: Delete a Multiple Rows by Index Position in DataFrame
Example #4: Delete rows from dataFrame in Place
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course