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


Pandas is one of the most popular python library for data analysis and data wrangling. In this article we will see how we can create a pandas dataframe and then delete some selective rows ort columns from this data frame.

Deleting roews

In the below example we have the iris.csv file which is read into a data frame. We first have a look at the existing data frame and then apply the drop function to the index column by supplying the value we want to drop. As we can see at the bottom of the result set the number of rows has been reduced by 3.

Example

import pandas as pd
# making data frame from csv file
data = pd.read_csv("E:\iris1.csv",index_col ="Id")
print(data)
# dropping passed values
data.drop([6,9,10],inplace=True)
# display
print(data)

Output

Running the above code gives us the following result −

   SepalLengthCm    SepalWidthCm    PetalLengthCm    PetalWidthCm    Species
Id
1    5.1                3.5             1.4             0.2       Iris-setosa
2    4.9                3.0             1.4             0.2       Iris-setosa
3    4.7                3.2             1.3             0.2       Iris-setosa
.   ..   …   .…   .…..……
[150 rows x 5 columns]

After Dropping
   SepalLengthCm    SepalWidthCm    PetalLengthCm    PetalWidthCm    Species
Id
1      5.1                3.5             1.4             0.2       Iris-setosa
2      4.9                3.0             1.4             0.2       Iris-setosa
3      4.7                3.2             1.3             0.2       Iris-setosa
149    6.2                3.4             5.4             2.3       Iris-virginica
150    5.9                3.0             5.1             1.8       Iris-virginica
………………….
[147 rows x 5 columns]

Dropping Columns

For dropping the columns form a pandas data frame, we use the axis parameter. Its value is set to one in the drop function and we supply the column names to be dropped. As you can see the number of columns in the result set gets reduced from 5 to 3.

Example

import pandas as pd
# making data frame from csv file
data = pd.read_csv("E:\iris1.csv",index_col ="Id")
print(data)
# dropping passed values
data.drop(['SepalWidthCm','PetalLengthCm'],axis=1,inplace=True)
print("After Dropping")
# display
print(data)

Output

Running the above code gives us the following result −

   SepalLengthCm    SepalWidthCm    PetalLengthCm    PetalWidthCm    Species
Id
1       5.1          3.5                1.4             0.2          Iris-setosa
2       4.9          3.0                1.4             0.2          Iris-setosa
3       4.7          3.2                1.3             0.2          Iris-setosa
.   .   .…   .…   .….   .……
[150 rows x 5 columns]
After Dropping
   SepalLengthCm    PetalWidthCm    Species
Id
1    5.1             0.2             Iris-setosa
2    4.9             0.2             Iris-setosa
3    4.7             0.2             Iris-setosa
.....….
[150 rows x 3 columns]

Updated on: 25-Jan-2021

696 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements