How to use drop() in pandas?

Mahitha Singirikonda
2 min readSep 27, 2020

--

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’)

Want to read this story later? Save it in Journal.

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

Dropping columns

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

Dropping rows

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.

Originally published at https://www.numpyninja.com on September 27, 2020.

--

--

Mahitha Singirikonda
Mahitha Singirikonda

Written by Mahitha Singirikonda

Data Scientist and Machine learning Engineer

No responses yet