Pandas provide data analysts with a way to delete and filter a data frame using .drop()
. With this method, rows or columns can be dropped using an index label or column name.
Syntax:
DataFrame.drop (labels = None, axis = 0, index = None, columns = None, level = None, inplace = False, errors = ’raise’)Parameters:
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 exist and drops rest of the values when errors = ’ignore’Return type: Dataframe with dropped values
To download the CSV used in the code, press here.
Example # 1: Deleting rows by index tag
the code is passed a list of index labels, and the lines matching those labels are removed using the .drop () method.
< code class = "undefined spaces"> |
Output:
As shown in output images, the new output has no passed values. These values were discarded and the changes were made to the original dataframe because inplace was True.
Dataframe before resetting values
Data frame after deleting values
Example # 2 : removing columns with column name
In its code, missing columns are removed using the column names. axis
is retained 1 because 1 refers to columns.
|
Output:
As shown in the output images, the new output is not has missing columns. These values were discarded because the axis was set to 1 and changes were made to the original dataframe because inplace was True.
Dataframe before deleting columns
Data frame after deleting columns