• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Drop First n Rows of pandas DataFrame

Drop First n Rows of pandas DataFrame

October 14, 2022 Leave a Comment

To drop the first n rows of a pandas DataFrame, the easiest way is with iloc.

import pandas as pd

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
                   'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
                   'Age': [43,23,71,49,52,37] })

print(df)
print(df.iloc[3:])

#Output:
    Name  Weight  Height  Age
0    Jim  130.54   50.10   43
1  Sally  160.20   68.94   23
2    Bob  209.45   71.42   71
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

    Name  Weight  Height  Age
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

You can also use the pandas DataFrame drop() function to get rid of the first n rows of a DataFrame.

import pandas as pd

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
                   'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
                   'Age': [43,23,71,49,52,37] })

print(df)
print(df.drop(df.index[0:3]))

#Output:
    Name  Weight  Height  Age
0    Jim  130.54   50.10   43
1  Sally  160.20   68.94   23
2    Bob  209.45   71.42   71
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

    Name  Weight  Height  Age
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

One last way you can delete the first n rows is with the pandas DataFrame tail() function.

import pandas as pd

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
                   'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
                   'Age': [43,23,71,49,52,37] })

print(df)
print(df.tail(-3))

#Output:
    Name  Weight  Height  Age
0    Jim  130.54   50.10   43
1  Sally  160.20   68.94   23
2    Bob  209.45   71.42   71
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

    Name  Weight  Height  Age
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

When working with data, the ability to remove and filter certain segments of your data easily is valuable.

One such case is if you want to remove the first n rows of a pandas DataFrame.

There are a few ways you can remove the first n rows of a pandas DataFrame.

These ways include using the pandas DataFrame iloc property, the pandas drop() Function and pandas tail() Function.

The rest of this post shows these three ways of dropping the first n rows of a pandas DataFrame.

Using iloc to Drop First Row of pandas DataFrame

The first way to drop the first n rows of a pandas DataFrame is with iloc.

The pandas DataFrame iloc property is a purely integer-location based indexing for selection by position.

With iloc, you can easily select rows that you want to work with.

To get rid of the first n rows of a pandas DataFrame with iloc, you can select everything except the first n rows by passing “[n:]”.

Below shows you how to use iloc to drop the first n rows in a pandas DataFrame.

import pandas as pd

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
                   'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
                   'Age': [43,23,71,49,52,37] })

print(df)
print(df.iloc[1:])

#Output:
    Name  Weight  Height  Age
0    Jim  130.54   50.10   43
1  Sally  160.20   68.94   23
2    Bob  209.45   71.42   71
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

    Name  Weight  Height  Age
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

Using drop() to Remove First n Rows of pandas DataFrame

Another way you can remove the first n rows of a pandas DataFrame is with the pandas drop() function.

drop() allows you to drop both rows and columns. By default, you can use drop() to delete rows of a pandas DataFrame.

To drop the first n rows, pass ‘df.index[0:n]’ to drop().

Below shows you how you can use drop() to remove the first n rows from a pandas DataFrame.

import pandas as pd

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
                   'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
                   'Age': [43,23,71,49,52,37] })

print(df)
print(df.drop(df.index[0:3]))

#Output:
    Name  Weight  Height  Age
0    Jim  130.54   50.10   43
1  Sally  160.20   68.94   23
2    Bob  209.45   71.42   71
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

    Name  Weight  Height  Age
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

Using tail() to Drop First n Rows of pandas DataFrame

One last way you can get rid of the first n rows of a pandas DataFrame is with the pandas tail() function.

The pandas tail() function allows us to get the last n rows of our DataFrame. By default, n is 5, but you can change this to any valid integer.

If you pass a negative number to tail(), you will get everything except the first n rows.

Below shows you how to use the tail() function to remove the first n rows of a pandas DataFrame.

import pandas as pd

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
                   'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
                   'Age': [43,23,71,49,52,37] })

print(df)
print(df.tail(-3))

#Output:
    Name  Weight  Height  Age
0    Jim  130.54   50.10   43
1  Sally  160.20   68.94   23
2    Bob  209.45   71.42   71
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

    Name  Weight  Height  Age
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

Hopefully this article has been useful for you to learn how to remove the first n rows of a pandas DataFrame in your Python code.

Other Articles You'll Also Like:

  • 1.  How to Rotate a List in Python
  • 2.  Sort List of Tuples in Python
  • 3.  Using Python to Add Trailing Zeros to String
  • 4.  Change Python Turtle Background Color with screensize() Function
  • 5.  Reverse a List in Python Without Reverse Function
  • 6.  Difference Between read(), readline() and readlines() in Python
  • 7.  Set Widths of Columns in Word Document Table with python-docx
  • 8.  Python Get First Word in String
  • 9.  Python Add Days to Date Using datetime timedelta() Function
  • 10.  Flatten List of Tuples in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy