×

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 set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.

Python for Loop Increment

Introduction In general, loops are employed for sequential traversal. It belongs to the definite iteration category. Definite iterations imply that the number of iterations is explicitly set in advance.  In this article,...

4 minutes read.

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

Python bin() function

Python bin() function The bin() function in Python returns the binary version for the specified integer. Syntax bin(x) Parameter n: This parameter represents an integer or int value. Return This function returns a binary string for the specified integer...

1 minute read.

Arithmetic Expressions in Python

What is Python Expression? Expressions are collections of operands and operators. Python expressions are translated by the Python interpreter into some value or outcome. In Python, an expression is made up...

13 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Python max() function

Python max() function The max() function in Python returns the largest item in an iterable or the largest of two or more arguments. Syntax max(iterable, *[, key, default])               or max(arg1, arg2, *args[, key])   Parameter arg1, arg2, *args: This...

1 minute read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 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.

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

Python Data Structures

Python Data Structures: Python is a programming language used worldwide for various fields such as building dynamic websites, artificial intelligence and many more. However, there is data that plays a...

18 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

Cube Root in Python

In general, the cube is a three-dimensional solid figure that has 6 square faces. It is also called a geometrical shape with six equal faces, eight vertices, and twelve edges....

3 minutes read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

Python Euclidean Distance

Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for...

1 minute read.

Important Difference between Python 2.x and Python 3.x with Example

The comparison between Python 2 and Python 3 is given in the article that follows. Python is a computer language that can perform more tasks than other languages and is...

5 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

Python md5() function

Python md5() function The md5() function in PHP calculates the md5 hash of a string. Syntax md5 ( string $str [, bool $raw_output] ) Parameter str(required)- This parameter specifies the string. raw_output(optional)- This parameter specifies a hex or binary output format: TRUE - Raw 16 character binary...

1 minute read.