Table of Contents
Different methods to drop rows in pandas DataFrame
In this tutorial we will discuss how to drop rows using the following methods:
- Drop a single Row in DataFrame by
Row Index Label
- Drop a multiple Rows in DataFrame by
Row Index Label
- Drop a single Row in DataFrame by
Row Index Position
- Drop a multiple Rows in DataFrame by
Row Index Position
- Drop Rows in a DataFrame with
conditions
Create pandas DataFrame with example data
DataFrame is a data structure used to store the data in two dimensional format. It is similar to table that stores the data in rows and columns. Rows represents the records/ tuples and columns refers to the attributes.
We can create the DataFrame by using pandas.DataFrame() method.
Syntax:
pandas.DataFrame(input_data,columns,index)
Parameters:
It will take mainly three parameters
- input_data is represents a list of data
columns
represent the columns names for the dataindex
represent the row numbers/values
We can also create a DataFrame using dictionary by skipping columns and indices.
Let’s see an example.
Example:
Python Program to create a dataframe for market data from a dictionary of food items by specifying the column names.
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#display the dataframe
print(dataframe)
Output:
id name cost quantity
item-1 foo-23 ground-nut oil 567.00 1
item-2 foo-13 almonds 562.56 2
item-3 foo-02 flour 67.00 3
item-4 foo-31 cereals 76.09 2
Method 1 - Drop a single Row in DataFrame by Row Index Label
Here we are going to delete/drop single row from the dataframe using index name/label
.
Syntax:
dataframe.drop('index_label')
where,
- dataframe is the input dataframe
- index_label represents the index name
Example 1: Drop last row in the pandas.DataFrame
In this example we are going to drop last row using row label
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#drop item-4
print(dataframe.drop('item-4'))
Output:
id name cost quantity
item-1 foo-23 ground-nut oil 567.00 1
item-2 foo-13 almonds 562.56 2
item-3 foo-02 flour 67.00 3
Example 2: Drop nth row in the pandas.DataFrame
In this example we are going to drop second row using row label
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#drop item-2
print(dataframe.drop('item-2'))
Output:
id name cost quantity
item-1 foo-23 ground-nut oil 567.00 1
item-3 foo-02 flour 67.00 3
item-4 foo-31 cereals 76.09 2
Method 2 - Drop multiple Rows in DataFrame by Row Index Label
Here we are going to delete/drop multiple rows from the dataframe using index name/label
. We have to use comma operator to separate the index_labels
though a list
Syntax:
dataframe.drop(['index_label',...............'index_label'])
where,
- dataframe is the input dataframe
index_label
represents the index name
Example 1: In this example, we are going to drop 2 nd and 4 th row
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#drop item-2 and item-4
print(dataframe.drop(['item-2','item-4']))
Output:
id name cost quantity
item-1 foo-23 ground-nut oil 567.0 1
item-3 foo-02 flour 67.0 3
Example 2: In this example, we are going to drop 1 st , 2 nd and 4 th row
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#drop item-1 , item-2 and item-4
print(dataframe.drop(['item-1','item-2','item-4']))
Output:
id name cost quantity
item-3 foo-02 flour 67.0 3
Method 3 - Drop a single Row in DataFrame by Row Index Position
Here we are going to delete/drop single row from the dataframe using index position
. we have to pass index by using index()
method. indexing starts with 0
.
Syntax:
dataframe.drop([dataframe.index[index_position]])
where,
- dataframe is the input dataframe
index_position
represents the index position
Example 1: In this example we are going to drop last row using row position
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#drop item-4
print(dataframe.drop([dataframe.index[3]]))
Output:
id name cost quantity
item-1 foo-23 ground-nut oil 567.00 1
item-2 foo-13 almonds 562.56 2
item-3 foo-02 flour 67.00 3
Example 2- In this example we are going to drop second row using row position
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#drop item-2
print(dataframe.drop([dataframe.index[1]]))
Output:
id name cost quantity
item-1 foo-23 ground-nut oil 567.00 1
item-3 foo-02 flour 67.00 3
item-4 foo-31 cereals 76.09 2
Method 4 - Drop multiple Rows in DataFrame by Row Index Position
Here we are going to delete/drop multiple rows from the dataframe using index Position
.
Syntax:
dataframe.drop([dataframe.index[index_position],...............,dataframe.index[index_position]])
where,
- dataframe is the input dataframe
index_position
represents the index position
Example 1: In this example, we are going to drop 2 nd and 4 th row
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#drop item-2 and item-4
print(dataframe.drop([dataframe.index[1],dataframe.index[3]]))
Output:
id name cost quantity
item-1 foo-23 ground-nut oil 567.0 1
item-3 foo-02 flour 67.0 3
Example 2: In this example, we are going to drop 1 st , 2 nd and 4 th row
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#drop item-1, item-2 and item-4
print(dataframe.drop([dataframe.index[0],dataframe.index[1],dataframe.index[3]]))
Output:
id name cost quantity
item-3 foo-02 flour 67.0 3
Method 5 - Drop Rows in a DataFrame with conditions
Now , we have to drop rows based on the conditions
.
Just specify the column name with a condition.
Syntax:
dataframe.drop(dataframe[dataframe['column'] operator value].index)
where,
- column refers the column name to be checked with
condition
operator
can be a relational operatorindex
is the function to drop row
Example 1: In this example, we are going to drop the rows based on cost column
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#drop rows based on cost column with value greater than 300
print(dataframe.drop(dataframe[dataframe['cost'] > 300].index))
Output:
id name cost quantity
item-3 foo-02 flour 67.00 3
item-4 foo-31 cereals 76.09 2
Example 2: In this example, we are going to drop the rows based on quantity column
#import the module
import pandas
#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
#pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
#drop rows nased on quantity column with value less than 3
print(dataframe.drop(dataframe[dataframe['quantity'] < 3].index))
#drop rows based on quantity column with value less than 1
print(dataframe.drop(dataframe[dataframe['quantity'] < 1].index))
Output:
id name cost quantity
item-3 foo-02 flour 67.0 3
id name cost quantity
item-1 foo-23 ground-nut oil 567.00 1
item-2 foo-13 almonds 562.56 2
item-3 foo-02 flour 67.00 3
item-4 foo-31 cereals 76.09 2
Summary
We discussed how to drop the row in the Pandas dataframe using four methods with index label
and index position
. We seen that drop
function is the common in all methods and we can also drop/delete the rows conditionally from the dataframe using column.
Further Reading