Pandas Drop Last N Rows From DataFrame

To drop the last n rows from the pandas DataFrame using either iloc[], drop(), slicing[] and head() methods. In this article, I will explain how to drop/remove the last n rows from Pandas DataFrame with examples.

1. Quick Examples of Drop Last N Rows From Pandas DataFrame

If you are in a hurry, below are some quick examples of how to drop the last n rows from DataFrame.


# Below are a quick examples
# Number of rows to drop
n = 2

# By using DataFrame.iloc[] to drop last n rows
df2 = df.iloc[:-n] 

# Using drop() function to delete last n rows
df.drop(df.tail(n).index,inplace = True)

# Slicing last n rows
df2 = df[:-n]

# Using DataFrame.head() function to drop last n rows
df2 = df.head(-n)

Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,24000],
    'Duration':['30days','40days','35days','60days'],
    'Discount':[1000,2300,2500,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

Yields below output.


    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500
r4   pandas  24000   60days      2000

2. Drop Last N Rows Using DataFrame.iloc[]

You can use DataFrame.iloc[] the indexing syntax [-n:] with n as an integer to select the last n rows from pandas DataFrame. Let’s drop the last two rows of df and store the resulting DataFrame as a separate pandas DataFrame. You can also use iloc[] to drop rows by Index from pandas DataFrame.


# By using DataFrame.iloc[] to drop last n rows
n = 2
df2 = df.iloc[:-n] 
print(df2)

Yields below output.


    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r2  PySpark  25000   40days      2300

3. Using DataFrame.drop() to Remove Last N Rows

You can also use DataFrame.drop() method to remove the last n rows. Use index param to specify the last index and inplace=True to apply the change on the existing DataFrame. For instance, df.drop(df.tail(n).index,inplace=True).


# Using drop() function to delete last n rows
n = 3
df.drop(df.tail(n).index,inplace = True)
print(df)

Yields below output.


   Courses    Fee Duration  Discount
r1   Spark  20000   30days      1000

4. Using Dataframe.slicing[] to Drop Last N Rows

Alternatively, You can also use df[:-n] to slicing the last n rows of pandas DataFrame.


# Slicing last n rows
n = 2
df2 = df[:-n]
print(df2)

Yields below output.


    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r2  PySpark  25000   40days      2300

5. Drop Last N Rows Using DataFrame.head() Function

You can also use df.head(-n) to delete the last n rows of pandas DataFrame. Generally, DataFrame.head() function is used to show the first n rows of a pandas DataFrame but you can pass a negative value to skip the rows from the bottom.


# Using DataFrame.head() function to drop last n rows
n = 2
df2 = df.head(-n)
print(df2)

Yields same output as above.

6. Complete Example For Drop Last N Rows From DataFrame


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,24000],
    'Duration':['30days','40days','35days','60days'],
    'Discount':[1000,2300,2500,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

# Number of rows to drop
n = 2
# By using DataFrame.iloc[] to drop last n rows
df2 = df.iloc[:-n] 
print(df2)

#  Number of rows to drop
n = 1
# Using drop() function to delete last n rows
df.drop(df.tail(n).index,
        inplace = True)
print(df)

# Number of rows to drop
n = 2
# Slicing last n rows
df2 = df[:-n]
print(df2)

#  Number of rows to drop
n = 2
#  Using DataFrame.head() function to drop last n rows
df2 = df.head(-n)
print(df2)

Conclusion

In this article, you have learned how to drop the last n rows From Pandas DataFrame using DataFrame.iloc[], DataFrame.drop(), DataFrame.head() and Dataframe.slicing[] function with examples.

Happy Learning !!

You May Also Like

References

Leave a Reply

Pandas Drop Last N Rows From DataFrame