×

Drop() Function in Python

Drop() Function:

The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and faster. The pandas provide a series or a dataframe to organize the data in a labelled format. The pandas library in python is faster and simple than any other library. The pandas are built over the Numpy for operating the python libraries; we require the Numpy.

The dataframe which is created in the pandas is mutable. That is, we can change the data present in the dataframe, we can insert the data, or can delete the data present in the dataframe. The pandas contain many built-in functions to perform various operations. One of them is the drop( ) function, used to drop or remove any column or rows present in the dataframe using the index of the columns or rows.

Syntax:

dataframe.drop(labels, axis, index, columns, level, in place., errors)

Parameters:

  • Labels: It provides the information about the labels or indexes to drop. We can take the input as a list if we want to drop more than one column or row.
  • Axis: It will provide the information about which axis we need to check. By default, it will be set as zero. If we want to drop a row, we need to provide it as column to remove the specified column. If we want to remove the row, we need to provide the axis as the index; it will drop the specified row. It is an optional parameter.
  • Index: It will provide the name of the rows to drop. It is also an optional parameter it can be used instead of the labels parameter.
  • Columns: It will provide the name of the columns to drop. It is also an optional parameter it can be used instead of the labels parameter.
  • Level: The dataframe may consist of the hierarchical tables so that it may consist of the multi-index; this parameter will provide the information about at which level we need to check in a multi-index dataframe. It is also an optional parameter; by default, it will be set as None.
  • In place: This parameter has only two values, True or False; by default, it will be set as False. If it is set as True, the removal operation is performed in the given dataframe. If it is False, it will return a copy of the column where the operation is performed.
  • Errors: It has only two values, ' raise’ and ‘ignore '. It is also an optional parameter; it will provide information on whether we need to consider the errors or not.
  • Return type: This function will return the dataframe with the removed values.

Examples:

  1. Dropping the rows:

 In this example, we will observe how we can drop the rows from the dataframe with the help of the index provided.

Code:

# importing pandas module
import pandas as pd
  
#Obtaining the data from the CSV file and creating the dataframe
data = pd.read_csv("nba.csv", index ="Name" )
  
# removing the values
data.drop(["Avery Bradley", "John Holland", "R.J. Hunter"], in place = True)
  
# print the dataframe after performing an operation
data

Output:

We can observe in the output that the updated dataframe doesn't contain the passed values; it only consists of the reaming data because the inplace is taken as True.

Before using the drop( ) function:

Drop( ) Function in Python

After using the drop( ) function:

Drop( ) Function in Python

2. Dropping the columns:

The columns are dropped from the dataframe by fixing the column as the label; the values are dropped from the dataframe with the help of the index values provided.

Code:

# importing pandas module
import pandas as pd
  
#Obtaining the data from the CSV file and creating the dataframe
data = pd.read_csv("nba.csv", col ="Name" )
  
# dropping passed columns from the dataframe
data.drop(["Team", "Weight"], axis = 1, inplace = True)
  
# print the dataframe
data

Output:

Here the axis is set as one, so it will drop the columns from the dataframe. Here, we can observe that the final output doesn’t contain the passed values, and the changes are made in the original dataframe because the inplace is True.

Before usingdrop( ) function:

Drop( ) Function in Python

After using  drop( ) function:

Drop( ) Function in Python

Related Topics

How to check if the dictionary is empty in Python?

What is a dictionary in Python? A directory is a collection of data but data is not ordered. Unlike other data types, it does not hold a single value as its...

3 minutes read.

Python set()

Python set() Class The set() class in Python returns a new set object, optionally with elements taken from iterable.  Syntax class set([iterable]) Parameter iterable : This parameter represents a sequence, collection or an iterator object Return This function returns a...

1 minute read.

Python vs PHP

Python Python is a high-level, object-oriented, interpreted language that is used to create independent program and algorithms for a variety of applications. It has a large library support base. In 1990,...

3 minutes read.

GUI Calculator in Python

Introduction In Python, we can develop a GUI(Graphical User Interface) with multiple options. It offers us some great and commonly used methods for the development of the Graphical User Interface. Tkinter...

4 minutes read.

Subprocess Module in Python

What is a Process? Every program will have its respective system state, i.e., the state in which it is running. A program that is in a running state or in a...

7 minutes read.

Python round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

Convert string into int in Python

String A string is defined as a series of characters, special characters, and numbers. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements...

2 minutes read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.

Python Dictionary update() method

Python Dictionary update() method The dictionary.update() method in Python inserts the specified items to the dictionary. Syntax dictionary.update(iterable) Parameter iterable- This parameter represents a dictionary or an iterable object with key value pairs, that will...

1 minute read.

Python String encode() method

Python String encode() method The string.encode() method in Python returns an encoded version of the string. The default encoding is the current default string encoding Syntax String.encode([encoding[,errors]]) Parameter Encoding: This parameter represents a String specifying...

2 minutes read.

JWT Decode Python

Python's PyJWT package makes it possible to encrypt and decrypt JSON Web Tokens (JWT). JWT is a public, recognized global benchmark for safely expressing demands between two parties (RFC 7519). JSON...

7 minutes read.

Compound Interest GUI Calculator using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Cross Entropy in Python

Introduction Cross-entropy loss is frequently combined with the softmax function. Determine the total entropy among the distributions or the cross-entropy, which is the difference between two probability distributions. For the purpose...

5 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Weight Conversion GUI using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

Python EOL (End Of Line)

Introduction An EOL (End Of Line) is defined as a syntax error that indicates that the Python interpreter reached at the end of the line when it tried to scan a...

3 minutes read.

What is Ipython shell?

IPython is a command shell for computing in multiple programming languages. IPython was developed for python language by Fernando Perez in 2001 as a well-equipped python interpreter that offers shell...

3 minutes read.