top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-
Writer's pictureMahitha Kumar

How to use drop() in pandas?


While processing real time data there will be many rows and columns which are not needed for the analysis, to keep the data set concise we need to drop them from data set.


Drop is one of the main functions used to cleanse data.We can drop specified labels from rows or columns by using drop() by mentioning corresponding axis,index or column names,level when using multi index labels on different levels.

Syntax for drop function

DataFrame.drop(self, labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

Parameters:



Example


let us see how to use drop through an example,

import numpy as np
import pandas as pd

The above code imports numpy and pandas libraries.


df = pd.DataFrame(np.arange(12).reshape(3, 4),columns=['A', 'B', 'C', 'D'])
print (df)

The above lines creates and prints a dataframe df with 4 columns and 3 rows arranging numbers from 0-11.The output is as follows:

   A  B   C   D
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

df = df.drop(['B', 'D'], axis=1)
print (df)

We are dropping columns B and D using the above line and printing the data frame.

Here we used axis=1 to specify that B and D are columns.The output is as follows:


   A   C
0  0   2
1  4   6
2  8  10

df = df.drop([1], axis = 0)
print (df)

With the above lines we are dropping row 1 from the above dataframe(df).The axis is 0 for rows.The out put will be as following:


   A   C
0  0   2
2  8  10


Conclusion

Hence Drop function has lot of importance in data cleansing pipelines.It is used heavily in pre-processing data as data can be dropped rows wise,column wise or on specified condition.


1,214 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page