The Wayback Machine - https://web.archive.org/web/20240802043809/https://www.geeksforgeeks.org/python-delete-rows-columns-from-dataframe-using-pandas-drop/
Open In App

Python | Delete rows/columns from DataFrame using Pandas.drop()

Last Updated : 19 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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 which makes importing and analyzing data much easier. In this article, we will how to delete a row in Excel using Pandas as well as delete a column from DataFrame using Pandas.

Pandas DataFrame drop() Method Syntax

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 exists and drops rest of the values when errors = ‘ignore’

Return type: Dataframe with dropped values

Python Drop Function in Pandas

Pandas provide data analysts with a way to delete and filter data frames using dataframe.drop() the method. Rows or columns can be removed using an index label or column name using this method.

Deleting Rows and Columns from Pandas DataFrame

Below are some ways and example by which we can delete a row in Excel using Pandas in Python.

Dropping Rows in Pandas by Index Label

In this code, A list of index labels is passed and the rows corresponding to those labels are dropped using .drop() method. To download the CSV used in the code, click here.

Python3




# importing pandas module
import pandas as pd
 
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col="Name")
print(data.head(5))


Output: Data Frame before Dropping values

  Name              Team  Number Position   Age Height  Weight            College     Salary               
Avery Bradley  Boston Celtics     0.0       PG  25.0    6-2   180.0              Texas  7730337.0
Jae Crowder     Boston Celtics     99.0       SF  25.0    6-6   235.0          Marquette  6796117.0
John Holland    Boston Celtics     30.0       SG  27.0    6-5   205.0  Boston University        NaN
R.J. Hunter        Boston Celtics     28.0       SG  22.0    6-5   185.0      Georgia State  1148640.0
Jonas Jerebko   Boston Celtics      8.0       PF  29.0   6-10   231.0                NaN  5000000.0

Applying the drop function.

Python3




# dropping passed values
data.drop(["Avery Bradley", "John Holland", "R.J. Hunter"], inplace = True)
 
# display
print(data)


Output: Data Frame after Dropping values

As shown in the output before, 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.

                         Team  Number Position   Age Height  Weight   College     Salary
Name                                                                  
Jae Crowder    Boston Celtics    99.0       SF  25.0    6-6   235.0 Marquette  6796117.0
Jonas Jerebko  Boston Celtics     8.0       PF  29.0   6-10   231.0       NaN  5000000.0 
Amir Johnson   Boston Celtics    90.0       PF  29.0    6-9   240.0       NaN 12000000.0 
Jordan Mickey  Boston Celtics    55.0       PF  21.0    6-8   235.0       LSU  1170960.0 
Kelly Olynyk   Boston Celtics    41.0        C  25.0    7-0   238.0   Gonzaga  2165160.0  

Dropping Columns in Pandas with Column Name

In this code, Passed columns are dropped using column names. axis parameter is kept 1 since 1 refers to columns.

Python3




# importing pandas module
import pandas as pd
   
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name" )
print(data.head())


Output: Data Frame before Dropping Columns

                         Team  Number Position   Age Height  Weight            College     Salary
Name                                                                  
Avery Bradley  Boston Celtics     0.0       PG  25.0    6-2   180.0              Texas  7730337.0
Jae Crowder    Boston Celtics    99.0       SF  25.0    6-6   235.0          Marquette  6796117.0
John Holland   Boston Celtics    30.0       SG  27.0    6-5   205.0  Boston University        NaN
R.J. Hunter    Boston Celtics    28.0       SG  22.0    6-5   185.0      Georgia State  1148640.0
Jonas Jerebko  Boston Celtics     8.0       PF  29.0   6-10   231.0                NaN  5000000.0

Applying drop function.

Python3




# dropping passed columns
data.drop(["Team", "Weight"], axis = 1, inplace = True)
   
# display
print(data.head())


Output: Data Frame after Dropping Columns

As shown in the output images, the new output doesn’t have the passed columns. Those values were dropped since the axis was set equal to 1 and the changes were made in the original data frame since inplace was True.

               Number Position   Age Height            College     Salary
Name                                                                     
Avery Bradley     0.0       PG  25.0    6-2              Texas  7730337.0
Jae Crowder      99.0       SF  25.0    6-6          Marquette  6796117.0
John Holland     30.0       SG  27.0    6-5  Boston University        NaN
R.J. Hunter      28.0       SG  22.0    6-5      Georgia State  1148640.0
Jonas Jerebko     8.0       PF  29.0   6-10                NaN  5000000.0


Previous Article
Next Article

Similar Reads

Drop rows from Pandas dataframe with missing values or NaN in columns
Pandas provides various data structures and operations for manipulating numerical data and time series. However, there can be cases where some data might be missing. In Pandas missing data is represented by two value: None: None is a Python singleton object that is often used for missing data in Python code. NaN: NaN (an acronym for Not a Number),
4 min read
Drop a list of rows from a Pandas DataFrame
Let us see how to drop a list of rows in a Pandas DataFrame. We can do this using the Pandas drop() function. We will also pass inplace = True and axis=0 to denote row, as it makes the changes we make in the instance stored in that instance without doing any assignment. Creating Dataframe to drop a list of rows C/C++ Code # import the module import
3 min read
How to Drop Columns with NaN Values in Pandas DataFrame?
Nan(Not a number) is a floating-point value which can't be converted into other data type expect to float. In data analysis, Nan is the unnecessary value which must be removed in order to analyze the data set properly. In this article, we will discuss how to remove/drop columns having Nan values in the pandas Dataframe. We have a function known as
3 min read
How to drop a level from a multi-level column index in Pandas Dataframe ?
In this article, we will learn about how to drop a level from a multi-level column index. But before that, we need to know what is a multi-level index. A multi-level index dataframe is a type of dataframe that contains multiple level or hierarchical indexing. In this article, we will be creating a dataframe of our own choice with multiple column in
5 min read
Drop specific rows from multiindex Pandas Dataframe
In this article, we will learn how to drop specific rows from the multi-index DataFrame. First, let us create the multi-index DataFrame. The steps are given below: C/C++ Code import numpy as np import pandas as pd mldx_arrays = [np.array(['lion', 'lion', 'lion', 'bison', 'bison', 'bison', 'hawk', 'hawk', 'hawk']), np.array(['height', 'weight', 'spe
3 min read
How to drop rows in Pandas DataFrame by index labels?
Pandas provide data analysts a way to delete and filter dataframe using the .drop() method. Rows can be removed using index labels or column names using this method. In this article, we will see how to drop rows in Pandas Dataframe by index labels. Pandas Drop Rows by IndexCreating a Simple Pandas Dataframe. Python3# import pandas library import pa
5 min read
How to Drop Rows with NaN Values in Pandas DataFrame?
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results. In this article, we will
4 min read
How to Find & Drop duplicate columns in a Pandas DataFrame?
Let’s discuss How to Find and drop duplicate columns in a Pandas DataFrame. First, Let’s create a simple Dataframe with column names 'Name', 'Age', 'Domicile', and 'Age'/'Marks'.  Find Duplicate Columns from a DataFrameTo find duplicate columns we need to iterate through all columns of a DataFrame and for each and every column it will search if any
4 min read
How to drop one or multiple columns in Pandas Dataframe
Let’s discuss how to drop one or multiple columns in Pandas Dataframe. To Delete a column from a Pandas DataFrame or Drop one or multiple columns in a Pandas Dataframe can be achieved in multiple ways. # Drop column 'B'df = df.drop('B', axis=1)# Drop columns multiple column df = df.drop(['B', 'C'], axis=1)There are various methods to drop one or mu
10 min read
How to Drop Negative Values in Pandas DataFrame
Handling data effectively is crucial in data analysis and manipulation. One common task is cleaning the data by removing negative values, especially when they are considered outliers or invalid entries. The Pandas library in Python offers several efficient methods to accomplish this. This article will provide three good examples of how to drop nega
3 min read
three90RightbarBannerImg