In this tutorial, we will look at how to drop the first n rows of a pandas dataframe.
How to drop the first n rows of a dataframe?
There are a number of ways to remove the first n rows of a dataframe. For example, you can slice the dataframe using .iloc
or you can use the pandas drop()
function or you can use the pandas tail()
function. The following is the syntax for the different methods used in this tutorial.
# using iloc df.iloc[n:] # using drop() df.drop(df.index[:n]) # using tail() df.tail(-n)
Examples
Let’s look at some examples of using the above methods to remove the first n rows of a dataframe. First we will create a sample dataframe that we will be using throughout this tutorial.
import pandas as pd # create dataframe df = pd.DataFrame({ 'Name': ['Bulbasaur', 'Charmander', 'Squirtle', 'Caterpie', 'Ekans'], 'Type': ['Grass', 'Fire', 'Water', 'Bug', 'Poison'] }) # display the dataframe print(df)
Output:
Name Type 0 Bulbasaur Grass 1 Charmander Fire 2 Squirtle Water 3 Caterpie Bug 4 Ekans Poison
We now have a dataframe with five rows containing information on Pokemon names and their respective types.
1. Remove first n rows with .iloc
You can drop the first n rows by slicing them using .iloc
. For example, let’s remove the first two rows of df and store the resulting dataframe as a separate dataframe.
# remove top two rows df_sub = df.iloc[2:] # display the dataframe print(df_sub)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Name Type 2 Squirtle Water 3 Caterpie Bug 4 Ekans Poison
The resulting dataframe has the top two rows removed. Note that, the slice 2:
refers to take all the rows starting from index 2 (that is, the third row). That is why the resulting dataframe has rows from index 2.
2. Remove first n rows with drop()
You can also use the pandas drop()
function to remove the first n rows of a dataframe. For this, pass the indexes of the rows you want to delete to the drop() function. For example, let’s again drop the top two rows of the dataframe df, this time using the drop() function.
# remove top two rows df_sub = df.drop(df.index[:2]) # display the dataframe print(df_sub)
Output:
Name Type 2 Squirtle Water 3 Caterpie Bug 4 Ekans Poison
The first two rows are dropped in the returned dataframe. If you want to modify the original dataframe in place, pass inplace=True
to the drop() function.
3. Remove first n rows with tail()
You can also use the pandas tail()
function to remove the first n rows. Generally, the tail() function is used to show the last n rows of a pandas dataframe but you can pass a negative value to skip the rows from the beginning.
# remove top two rows df_sub = df.tail(-2) # display the dataframe print(df_sub)
Output:
Name Type 2 Squirtle Water 3 Caterpie Bug 4 Ekans Poison
You can see that the resulting dataframe has all the rows from the bottom except for the first two rows. This is because we passed -2 to the tail() function which equates to performing df[2:]
.
For more on the pandas tail() function, refer to its documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on removing data from pandas dataframe –