Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas provide data analysts a way to delete and filter data frame using
.drop() method. Rows or columns 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
To download the CSV used in code, click here.
Example #1: Dropping Rows by index label
In his code, A list of index labels is passed and the rows corresponding to those labels are dropped using .drop() method.
As shown in the output images, the new output doesn’t have the passed values. Those values were dropped and the changes were made in the original data frame since inplace was True.
Data Frame before Dropping values-
Data Frame after Dropping values-
Example #2 : Dropping columns with column name
In his code, Passed columns are dropped using column names.
axis parameter is kept 1 since 1 refers to columns.
As shown in the output images, the new output doesn’t have the passed columns. Those values were dropped since axis was set equal to 1 and the changes were made in the original data frame since inplace was True.
Data Frame before Dropping Columns-
Data Frame after Dropping Columns-
- Python | Pandas DataFrame.fillna() to replace Null values in dataframe
- Python | Pandas DataFrame.ix[ ]
- Python | Pandas dataframe.mod()
- Python | Pandas dataframe.mad()
- Python | Pandas Dataframe.at[ ]
- Python | Pandas Dataframe.iat[ ]
- Python | Pandas dataframe.add()
- Python | Pandas dataframe.min()
- Python | Pandas dataframe.sum()
- Python | Pandas DataFrame.abs()
- Python | Pandas dataframe.take()
- Python | Pandas dataframe.get()
- Python | Pandas dataframe.sub()
- Python | Pandas dataframe.sem()
- Python | Pandas dataframe.eq()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.