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.

Categorized in:

Python,

Last Update: March 4, 2024