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
- Delete a column from a Pandas DataFrame
- Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- Delete a directory or file using Python
- Delete an entire directory tree using Python | shutil.rmtree() method
- Delete Google Browser History using Python
- How to Delete files in Python using send2trash module?
- Check if a value exists in a DataFrame using in & not in operator in Python-Pandas
- Select first or last N rows in a Dataframe using head() and tail() method in Python-Pandas
- How to Remove repetitive characters from words of the given Pandas DataFrame using Regex?
- Create pandas dataframe from lists using zip
- Create pandas dataframe from lists using dictionary
- Creating a dataframe using Excel files
- Creating a Pandas dataframe using list of tuples
- Split a String into columns using regex in pandas DataFrame
- Create a column using for loop in Pandas Dataframe
- Reshape a pandas DataFrame using stack,unstack and melt method
- Using dictionary to remap values in Pandas DataFrame columns
- Construct a DataFrame in Pandas using string data
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.